C# Error CS1939 – Cannot pass the range variable ‘{0}’ as an out or ref parameter

C# Error

CS1939 – Cannot pass the range variable ‘{0}’ as an out or ref parameter

Reason for the Error & Solution

Cannot pass the range variable ‘name’ as an out or ref parameter.

A range variable is a read-only variable that is introduced in a query expression and serves as an identifier for each successive element in a source sequence. Because it cannot be modified in any way, there is no point in passing it by ref or out. Therefore, both operations are not valid.

To correct this error

  1. Pass the range variable by value.

Example

The following example generates CS1939:

// cs1939.cs  
using System.Linq;  
class Test  
{  
    public static void F(ref int i)  
    {  
    }  
    public static void Main()  
    {  
        var list = new int[] { 0, 1, 2, 3, 4, 5 };  
        var q = from x in list  
                let k = x  
                select Test.F(ref x); // CS1939  
    }  
}  

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