HomeCSharpC# 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.

C# 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.

C# 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.

Reason for the Error & Solution

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  
   }  
}  

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...