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

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