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

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...