HomeCSharpC# 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

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

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