Information Technology Reference
In-Depth Information
For example, the following code implements the ColorEnumerator example using the
generic enumerator interface:
using System.Collections;
using System.Collections.Generic; Substitute type for T
class ColorEnumerator : IEnumerator<string>
{
string[] Colors; int Position = -1;
Returns a derived type
public string Current // Current--generic
{ get { return Colors[Position]; } }
Explicit implementation
object IEnumerator.Current // Current--non-generic
{ get { return Colors[Position]; } }
public bool MoveNext() // MoveNext
{
if (Position < Colors.Length - 1)
{ Position++; return true; }
else
return false;
}
public void Reset() // Reset
{ Position = -1; }
public void Dispose() { }
public ColorEnumerator(string[] colors) // Constructor
{
Colors = new string[colors.Length];
for (int i = 0; i < colors.Length; i++)
Colors[i] = colors[i];
}
}
Search WWH ::




Custom Search