C# Error CS1930 – The range variable ‘{0}’ has already been declared

C# Error

CS1930 – The range variable ‘{0}’ has already been declared

Reason for the Error & Solution

The range variable ‘name’ has already been declared

The range variable in a query expression is in scope until the query expression terminates. It must therefore have a unique identifier.

To correct this error

  1. Give a unique name to each range variable that is introduced in a query expression.

Example

The following example generates CS1930 because the identifier num is used for the range variable in the from clause and for the range variable introduced by the let clause.

// cs1930.cs  
using System.Linq;  
class Program  
{  
    static void Main()  
    {  
        int[] nums = { 0, 1, 2, 3, 4, 5 };  
        var query = from num in nums  
                    let num = 3 // CS1930  
                    select num;
    }  
}  

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