Share via


Errore del compilatore CS1621

Aggiornamento: novembre 2007

Messaggio di errore

L'istruzione yield non può essere utilizzata all'interno di un metodo anonimo o un'espressione lambda
The yield statement cannot be used inside an anonymous method or lambda expression

Non è possibile utilizzare l'istruzione yield all'interno di un blocco del metodo anonimo di un iteratore.

Esempio

Il seguente codice di esempio genera l'errore 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()
    {
    }
}