更新:2007 年 11 月
错误消息
不能在匿名方法或 lambda 表达式内使用 yield 语句
yield 语句不能存在于迭代器的匿名方法块中。
示例
下面的示例生成 CS1621:
// CS1621.cs
using System.Collections;
delegate object MyDelegate();
class C : IEnumerable
{
public IEnumerator GetEnumerator()
{
MyDelegate d = delegate
{
yield return this; // CS1621
return this;
};
d();
// Try this instead:
// MyDelegate d = delegate { return this; };
// yield return d();
}
public static void Main()
{
}
}