C# Error CS1931 – The range variable ‘{0}’ conflicts with a previous declaration of ‘{0}’

C# Error

CS1931 – The range variable ‘{0}’ conflicts with a previous declaration of ‘{0}’

Reason for the Error & Solution

The range variable ‘variable’ conflicts with a previous declaration of ‘variable’.

The declaration of a range variable, just like every other declaration, must have an identifier which is unique within the variable’s declaration space.

To correct this error

  1. Give the range variable a unique name.

Example

The following code generates CS1931 because the identifier x is used both as a local variable in Main and as the range variable in the query expression:

// cs1931.cs  
class Test  
    {  
        static void Main()  
        {  
            int x = 1;  
            var y = from x in Enumerable.Range(1, 100) // CS1931  
                    select x;  
        }  
    }  

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