我正在尝试这个MSDN 页面上的示例。我试图改变GetEnumerator
方法。我知道这似乎有些不对劲,但是它符合要求,然后就无法运行。错误是枚举器尚未启动,MoveNext
应该调用它,但它正在被调用!
class Program
{
static void Main(string[] args)
{
foreach (var day in new DaysOfTheWekk())
{
Console.WriteLine(day) ;
}
Console.ReadLine();
}
}
public class DaysOfTheWekk: IEnumerable
{
private string[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
public IEnumerator GetEnumerator()
{
days.GetEnumerator().MoveNext();
yield return days.GetEnumerator().Current;
}
}