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

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