C# Error CS1623 – Iterators cannot have ref, in or out parameters

C# Error

CS1623 – Iterators cannot have ref, in or out parameters

Reason for the Error & Solution

Iterators cannot have in ref or out parameters

This error occurs if an iterator method takes an in, ref, or out parameter. To avoid this error, remove the in, ref, or out keyword from the method signature.

Example

The following sample generates CS1623:

// CS1623.cs  
using System.Collections;

class C : IEnumerable
{
    public IEnumerator GetEnumerator()
    {
        yield return 0;
    }

    // To resolve the error, remove in  
    public IEnumerator GetEnumerator(in short i)  // CS1623  
    {
        yield return i;
    }
    // To resolve the error, remove ref  
    public IEnumerator GetEnumerator(ref int i)  // CS1623  
    {
        yield return i;
    }

    // To resolve the error, remove out  
    public IEnumerator GetEnumerator(out float f)  // CS1623  
    {
        f = 0.0F;
        yield return f;
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

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