gpt4 book ai didi

c# - 如何在函数中传递 IList 参数

转载 作者:行者123 更新时间:2023-11-30 19:47:16 24 4
gpt4 key购买 nike

我有这个功能:

public static IList<T> myFunction<T>(IList<T> listaCompleta, int numeroPacchetti)
{
return listaCompleta;
}

但是如果我尝试用 :

调用它
IList<SomeObject> listPacchetti = (from SomeObject myso in SomeObjects
select myso).ToList();

listPacchetti = myFunction(listPacchetti, 1);

当我编译时它说 The type arguments for method 'myFunction<T>(IList<T>, int)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

事实是我需要使用 IList(或集合索引,而不是 IEnumerable)并且我需要将通用对象传递给函数(这次是 IList<SomeObject>,下一次可能是 IList<AnotherObject>)

我可以吗?或者怎么办?我认为我不能将 IList 用作类型参数...

编辑 - 完整代码

我调用另一个类的函数

IList<Packet> listPacchetti = (from Packet pack in Packets
select pack).ToList();

listPacchetti = Utility.Extracts<Packet>(listPacchetti, 6);

具有功能的类

public class Utility
{
public Utility()
{

}

public static IList<T> Extracts<T>(IList<T> listaCompleta, int numeroPacchetti) // HERE THERE IS THE LINE WITH WARNINGS
{
return listaCompleta;
}
}

最佳答案

您的实用程序类是否针对 .NET Framework 3.0 或更高版本编译,您的代码是否引用了 System.Collections.Generic 命名空间?

那么,是不是 using System.Collections.Generic; 不见了?

关于c# - 如何在函数中传递 IList<T> 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7022512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com