C# Error CS1949 – The contextual keyword ‘var’ cannot be used in a range variable declaration

C# Error

CS1949 – The contextual keyword ‘var’ cannot be used in a range variable declaration

Reason for the Error & Solution

The contextual keyword ‘var’ cannot be used in a range variable declaration.

A range variable is implicitly typed by the compiler. There is no need to use with a range variable.

To correct this error

  1. Remove the var keyword from in front of the range variable.

Example

The following example generates CS1949:

// cs1949.cs  
using System;  
using System.Linq;  
class Test  
{  
    static void Main()  
    {  
        var x = from var i in Enumerable.Range(1, 100) // CS1949  
        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...