Information Technology Reference
In-Depth Information
The following code shows a pattern for implementing the generic interface. T is the type
returned by the enumerator.
using System.Collections;
using System.Collections.Generic;
class MyGenEnumerable: IEnumerable<T>
{
public IEnumerator<T> GetEnumerator() { ... } // IEnumerable<T> version
Explicit implementation
IEnumerator IEnumerable.GetEnumerator() { ... } // IEnumerable version
...
}
For example, the following code shows the use of the generic enumerable interface:
using System.Collections;
using System.Collections.Generic;
Substitute actual type for T
class MyColors : IEnumerable<string>
{
string[] Colors = { "Red", "Yellow", "Blue" };
Substitute actual type for T
public IEnumerator<string> GetEnumerator() // IEnumerable<T> version
{
return new ColorEnumerator(Colors);
}
Explicit implementation
IEnumerator IEnumerable.GetEnumerator() // IEnumerable version
{
return new ColorEnumerator(Colors);
}
}
Search WWH ::




Custom Search