I have a code:
public sealed class Sequence : IEnumerable<MyClass&g开发者_Python百科t;
{
List<MyClass> _elements;
public IEnumerator<MyClass> Getenumerator()
{
foreach (var item in _elements)
{
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this._elements.GetEnumerator();
}
}
// ....
Sequence s = new Sequence();
// ...
// filling s with some data
// ...
foreach(MyClass c in s)
{
// some action
}
That code doesn't want to compile. It DOESNT want to compile. Using the generic type 'System.Collections.Generic.IEnumerator' requires '1' type arguments
Help me to rewrite MyClass to support enumeration.
I tried several combinations, used Google. Everywhere examples of Enumerable and IEnumerator without generics.
Add the line:
using System.Collections;
You want to use System.Collections.IEnumerator
, not System.Collections.Generic.IEnumerator<T>
.
Sounds like you're just missing a using
directive, since IEnumerator
(of the non-generic variety) lives in System.Collections
, and not System.Collections.Generic
(which is included by default at the top of every source file).
So just define:
using System.Collections;
ando you're good to go.
精彩评论