更新:2007 年 11 月
错误消息
foreach 要求“type.GetEnumerator()”的返回类型“type”必须有合适的公共 MoveNext 方法和公共 Current 属性用于启用使用 foreach 语句的函数
在 C# 2.0 中,编译器将自动为您生成 Current 和 MoveNext。有关更多信息,请参见 |
下面的示例生成 CS0202:
// CS0202.cs
public class C1
{
public int Current
{
get
{
return 0;
}
}
public bool MoveNext ()
{
return false;
}
public static implicit operator C1 (int c1)
{
return 0;
}
}
public class C2
{
public int Current
{
get
{
return 0;
}
}
public bool MoveNext ()
{
return false;
}
public C1[] GetEnumerator ()
// try the following line instead
// public C1 GetEnumerator ()
{
return null;
}
}
public class MainClass
{
public static void Main ()
{
C2 c2 = new C2();
foreach (C1 x in c2) // CS0202
{
System.Console.WriteLine(x.Current);
}
}
} | |