Error del compilador CS1621

La instrucción yield no se puede usar dentro de un método anónimo o una expresión lambda

No se puede usar la instrucción yield en un método anónimo ni en una expresión lambda.

El ejemplo siguiente genera la advertencia 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()  
    {  
    }  
}  

Consulte también