C# Error CS1932 – Cannot assign {0} to a range variable

C# Error

CS1932 – Cannot assign {0} to a range variable

Reason for the Error & Solution

Cannot assign ‘expression’ to a range variable.

The compiler must be able to infer the type of a range variable, whether it is introduced in a from clause or a let clause. It cannot be null because null is not a type, and it cannot be assigned with an expression of an unsafe type.

To correct this error

  • Remove the assignment that is not valid.

  • Explicitly cast the expression to an allowed type

Example

The following code generates CS1932 because the type of the range variable cannot be inferred. Cast the value to the intended type to fix the error, as shown in the following example.

// CS1932.cs  
using System.Linq;  
class Test  
{  
    static void Main()  
    {  
  
        var x = from i in Enumerable.Range(1, 100)  
                let k = null // CS1932  
                // Try the following line instead.  
                let k = (string) null  
                select i;  
    }  
}  

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