I have a this code
public class SomeClass<T>: IEnumerable<T>
{
public List<SomeClass<T>> MyList = new List<SomeClass<T>>();
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
}
How can I Extract a IEnumerator from MyList ?
Thanks StackoverFlower....
This:
public List<SomeClass<T>> MyList = new List<SomeClass<T>>();
Needs to be this:
public List<T> MyList = new List<T>();
then, this should work:
public IEnumerator<T> Getenumerator ()
{
foreach (var item in MyList){
yield return item;}
}
You can't have a
List<SomeClass<T>>
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>>
and change the Enumerator to be
public IEnumerator<SomeClass<T>> Getenumerator ()
{
foreach (var item in MyList){
yield return item;}
}
The trivial option would be return MyList.GetEnumerator().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With