Information Technology Reference
In-Depth Information
IEnumerator<string> Colors // Property--enumerator iterator
{
get {
string[] TheColors = { "blue", "red", "yellow" };
for (int i = 0; i < TheColors.Length; i++)
yield return TheColors[i];
}
}
public IEnumerator<string> GetEnumerator() // GetEnumerator
{
return ColorFlag
? Colors // Return Colors enumerator
: BlackAndWhite; // Return BlackAndWhite enumerator
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return ColorFlag
? Colors // Return Colors enumerator
: BlackAndWhite; // Return BlackAndWhite enumerator
}
}
class Program
{
static void Main()
{
MyClass mc1 = new MyClass( true ); // Call constructor with true
foreach (string s in mc1)
Console.Write("{0} ", s);
Console.WriteLine("");
MyClass mc2 = new MyClass( false ); // Call constructor with false
foreach (string s in mc2)
Console.Write("{0} ", s);
Console.WriteLine("");
}
}
This code produces the following output:
blue red yellow
black gray white
Search WWH ::




Custom Search