C# Error CS0136 – A local variable named ‘var’ cannot be declared in this scope

C# Compiler Error

CS0136 – A local variable named ‘var’ cannot be declared in this scope because it would give a different meaning to ‘var’, which is already used in a ‘parent or current/child’ scope to denote something else

Reason for the Error

You will receive this error in your C# code when a variable that you have declared hides another declaration with-in the same scope.

For example, try compiling the below code snippet.

namespace DeveloperPublishNamespace
{
    public class DeveloperPublish
    {
        public static int i = 0;
        public static void Main()
        {
            int var1 = 0;
            {
                int var1 = 78;  
            }
            var1++;
        }
    }

}

You will receive the below error as the variable var1 is already declared with-in the Main() function. The 2nd declaration of var1 results in this error.

C# Error CS0136 – A local variable named 'var' cannot be declared in this scope

Error CS0136 A local or parameter named ‘var1’ cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter ConsoleApp1 C:\Users\SenthilBalu\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 10 Active

Solution

To fix the error code CS0136, you will need to rename the variable that is causing issues.

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