コンパイラ エラー CS1621

更新 : 2007 年 11 月

エラー メッセージ

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()
    {
    }
}