[英]returning a generic IEnumerable<T>
I'm messing around with generics and IEnumerable abit, but i'm kindof stuck. 我搞混了泛型和IEnumerable,但是我有点卡住了。 Here's what i'm trying to do: I want to have a method that returns any collection type - that implements IEnumerable (?) - (so eg: List, Stack, Queue, ...)
这是我要尝试的操作:我想有一个返回任何集合类型的方法-实现IEnumerable(?)-(例如:List,Stack,Queue等)
furthermore, i want to be able to return any collection type, of any datatype. 此外,我希望能够返回任何数据类型的任何集合类型。 so i want this method to be able to return a
List<string>,
as well as a Stack<int>
, as well as a List<double>
... etc etc. 所以我希望这个方法能够返回
List<string>,
以及Stack<int>
以及List<double>
...等。
public IEnumerable<T> returnSomething()
{
Stack<int> stackOfInts = new Stack<int>();
List<string> listOfStrings = new List<string>();
return stackOfInts;
}
this is what i've tried so far. 到目前为止,这是我尝试过的。 this however doesn't work, i get this error:
但是这不起作用,我得到这个错误:
Cannot implicitly convert type 'System.Collections.Generic.Stack<int>' to 'System.Collections.Generic.IEnumerable<T>'. An explicit conversion exists (are you missing a cast?)
however, if i replace the IEnumerable<T>
in the method signature to IEnumerable<int>
, i can return any collection of type int. 但是,如果我将方法签名中的
IEnumerable<T>
替换为IEnumerable<int>
,则可以返回int类型的任何集合。 This however means, that now i can't return the ListOfStrings
anymore. 但是,这意味着现在我无法再返回
ListOfStrings
。
would appreciate any suggestions or ideas :) 将不胜感激任何建议或想法:)
You need to add a generic type parameter to your method: 您需要在方法中添加通用类型参数 :
public IEnumerable<T> ReturnSomething<T>()
{
Stack<T> stackOfT = new Stack<T>();
return stackOfT;
}
The type parameter appears after the method name, but before the parameters. 类型参数出现在方法名称之后,但在参数之前。 It is also possible to have a method with more than one type parameter.
也可能有一种具有多个类型参数的方法。
When you call the method you can specify the type: 调用方法时,可以指定类型:
IEnumerable<int> myInts = ReturnSomething<int>();
The trick is to declare <T>
right, if you define generic <T>
, then you have to stick to it in your methods, so if you have IEnumerable<T>
then elsewhere in your method you must use <T>
and not <int>
or any other type. 诀窍是要正确声明
<T>
,如果您定义通用<T>
,则必须在方法中坚持使用它,因此,如果您具有IEnumerable<T>
则方法的其他地方必须使用<T>
而不是<int>
或任何其他类型。
It is only latter when you actually use you generic type you substitute generic <T>
for a real type. 仅当您实际使用泛型类型时,才用泛型
<T>
代替实型。
See a sample 查看样本
class Foo<T>
{
public IEnumerable<T> GetList()
{
return new List<T>();
}
public IEnumerable<T> GetStack()
{
return new Stack<T>();
}
}
class Program
{
static void Main(string[] args)
{
Foo<int> foo = new Foo<int>();
IEnumerable<int> list = foo.GetList();
IEnumerable<int> stack = foo.GetStack();
Foo<string> foo1 = new Foo<string>();
IEnumerable<string> list1 = foo1.GetList();
IEnumerable<string> stack1 = foo1.GetStack();
}
}
public IEnumerable<T> returnSomething()
{
Stack<int> stackOfInts = new Stack<int>();
return (IEnumerable<T>) stackOfInts;
}
The type parameter needs to be specified by the caller somewhere. 类型参数需要由调用方在某处指定。
Either when instantiating a generic class: 实例化泛型类时:
public class MyClass<T>
{
public IEnumerable<T> returnSomething()
{
Stack<T> stackOfTs = new Stack<T>();
List<T> listOfTs = new List<T>();
return stackOfTs;
}
}
var v = new MyClass<int>();
foreach(var item in v.returnSomething())
{
}
Or when calling a generic method of a non-generic class: 或在调用非泛型类的泛型方法时:
public class MyClass
{
public IEnumerable<T> returnSomething<T>()
{
Stack<T> stackOfTs = new Stack<T>();
List<T> listOfTs = new List<T>();
return stackOfTs;
}
}
var v = new MyClass();
foreach(var item in v.returnSomething<int>())
{
}
Yes you can return any type if you change I Enumerable<T> to IEnumerable<dynamic>
是的,如果将I
Enumerable<T> to IEnumerable<dynamic>
更改Enumerable<T> to IEnumerable<dynamic>
则可以返回任何类型。
like this: 像这样:
public IEnumerable<dynamic> returnSomething()
{
.....
For more help full structure given below ... 如需更多帮助,请参见下面的完整结构...
my model is 我的模特是
public class Student
{
public int studentId { get; set; }
public string studentName { get; set; }
public string subject { get; set; }
public string studentClass { get; set; }
public int RollNumber { get; set; }
}
IEnumerable return datalist IEnumerable返回数据列表
public static IEnumerable<Student> ReturnSomething()
{
IList<Student> studentList = new List<Student>()
{
new Student()
{studentId = 1, studentName = "Bill", subject = "Science", studentClass = "nine", RollNumber = 01},
new Student()
{studentId = 2, studentName = "Steve", subject = "Arts", studentClass = "ten", RollNumber = 03},
new Student()
{studentId = 3, studentName = "Ram", subject = "Commerce", studentClass = "nine", RollNumber = 05},
new Student()
{studentId = 1, studentName = "Moin", subject = "Science", studentClass = "ten", RollNumber = 06}
};
return studentList;
}
and last one is access code 最后一个是访问代码
Student student = new Student();
IEnumerable<Student> studentList = ReturnSomething();
foreach (Student VARIABLE in studentList)
{
student.studentName += VARIABLE.studentName + " "+ "Class= ";
student.studentClass += VARIABLE.studentClass + " ";
}
Console.WriteLine(student.studentName + student.studentClass);
Console.ReadKey();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.