This documentation is archived and is not being maintained.

Compiler Error CS1622

Cannot return a value from an iterator. Use the yield return statement to return a value, or yield break to end the iteration.

An iterator is a special function that returns a value via the yield statement rather than the return statement. For more information, see iterators.

The following sample generates CS1622:

// CS1622.cs
// compile with: /target:library
using System.Collections;

class C : IEnumerable
{
   public IEnumerator GetEnumerator()
   {
      return (IEnumerator) this;  // CS1622
      yield return this;   // OK
   }
}
Show: