共用方式為


編譯器錯誤 CS1621

更新:2007 年 11 月

錯誤訊息

在匿名方法或 Lambda 運算式內部不能使用 yield 陳述式

yield 陳述式不能在 Iterator 的匿名方法區塊中。

範例

下列範例會產生 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()
    {
    }
}