C# IEnumerable
我想实现以下功能C# IEnumerable,c#,c#-3.0,C#,C# 3.0,我想实现以下功能 private static IEnumerable<T> Read() { ... } 如果我试图编译该项目,则会发生编译器错误 找不到类型 有人能帮我解决这个问题吗?问题是您返回的是IEnumerable,但没有指定T。您的签名应该如下所示: private static IEnumerable<T> Read<T>() { ... } 或者可能 using System.Collections.Generic; 当你这样说的时候:
private static IEnumerable<T> Read()
{
...
}
如果我试图编译该项目,则会发生编译器错误
找不到类型
有人能帮我解决这个问题吗?问题是您返回的是IEnumerable,但没有指定T。您的签名应该如下所示:
private static IEnumerable<T> Read<T>()
{
...
}
或者可能
using System.Collections.Generic;
当你这样说的时候:
private static IEnumerable<T> Read() {...}
您需要做以下四件事之一: 添加到源文件的顶部:
using System.Collections.Generics;
将该方法包含在泛型类中:
public class MyClass<T> {
// T is now the type of MyClass<T>
private static IEnumerable<T> Read() {...}
}
class MyClass<T> {
private IEnumerable<T> Read() {
//
}
}
你将需要一个两者的参考。Collections定义IEnumerable和Collections。Generic定义IEnumerableHe可以删除对System.Collections.Generic的引用,只使用IEnumerable无泛型参数来解决问题。是的,完全忽略了这一点。还有其他选项,包括将其放入泛型类或定义特定类型,例如IEnumerable Read…所有可能选项的良好概述。请看,这就是为什么您有47k,而我只有13k的原因:好你比我少活了几个月,现在你有三倍的代表。。。是的,我想我可以称之为痴迷
private static IEnumerable<T> Read<T>() { ... }
var results = MyClass.Read<int>();
private static IEnumerable<int> Read() { ... }
var results = MyClass.Read();
using System.Collections.Generics;
class MyClass<T> {
private IEnumerable<T> Read() {
//
}
}
private IEnumerable<T> Read<T>() {
//
}
private IEnumerable<string> Read() {
//
}