HomeCSharpC# Error CS1947 – Range variable ‘{0}’ cannot be assigned to — it is read only

C# Error CS1947 – Range variable ‘{0}’ cannot be assigned to — it is read only

C# Error

CS1947 – Range variable ‘{0}’ cannot be assigned to — it is read only

Reason for the Error & Solution

Range variable ‘variable name’ cannot be assigned to — it is read only.

A range variable is like an iteration variable in a foreach statement. It cannot be assigned to in a query expression.

To correct this error

  1. Remove the assignment to the range variable.

  2. If necessary, introduce a new range variable by using the let clause and use it to store the value.

Example

The following code generates CS1947:

// cs1947.cs  
using System.Linq;  
class Test  
{  
    static void Main()  
    {  
        int[] array = new int[] { 1, 2, 3, 4, 5 };  
        var x = from i in array  
                let k = i  
                select i = 5; // CS1947  
        x.ToList();  
    }  
}  

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