迭代器
using System;
using System.Collections;
namespace Enumerate
{
public class DaysOfTheWeek : System.Collections.IEnumerable
{
string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
public System.Collections.IEnumerator GetEnumerator()
{
for (int i = 0; i < m_Days.Length; i++)
{
yield return m_Days[i];
}
//yield return m_Days[0];
//yield return m_Days[1];
//yield return m_Days[2];
//yield return m_Days[3];
//yield return m_Days[4];
//yield return m_Days[5];
// yield return m_Days[6];
}
}
class TestDaysOfTheWeek
{
static void Main()
{
// Create an instance of the collection class
DaysOfTheWeek week = new DaysOfTheWeek();
// Iterate with foreach
foreach (string day in week)
{
System.Console.Write(day + " ");
}
Console.Read();
}
}
}