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

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