本文介绍了IEnumerator的< T>实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个code
public class SomeClass<T>: IEnumerable<T>
{
public List<SomeClass<T>> MyList = new List<SomeClass<T>>();
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
}
我如何可以提取的IEnumerator从MYLIST?
How can I Extract a IEnumerator from MyList ?
由于StackoverFlower ....
Thanks StackoverFlower....
推荐答案
本:
public List<SomeClass<T>> MyList = new List<SomeClass<T>>();
需求是这样的:
public List<T> MyList = new List<T>();
那么,这应该工作:
then, this should work:
public IEnumerator<T> Getenumerator ()
{
foreach (var item in MyList){
yield return item;}
}
您不能有一个
List<SomeClass<T>>
这是你拉的枚举,因为你在枚举器将返回℃的枚举项接口指定; T&GT;
。您还可以更改的IEnumerable&LT; T&GT;
是
that you pull the enumerator for, because you have specified in the interface that the enumerator will return an enumerable item of <T>
. You can also change IEnumerable<T>
to be
IEnumerable<SomeClass<T>>
和改变枚举为
public IEnumerator<SomeClass<T>> Getenumerator ()
{
foreach (var item in MyList){
yield return item;}
}
这篇关于IEnumerator的&LT; T&GT;实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!