Compartir a través de


Error del compilador CS1621

Actualización: noviembre 2007

Mensaje de error

La instrucción yield no se puede utilizar dentro de un método anónimo o una expresión lambda
The yield statement cannot be used inside an anonymous method or lambda expression

La instrucción yield no puede estar en un bloque de método anónimo de un iterador.

Ejemplo

El código siguiente genera el error 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()
    {
    }
}