Studies show that good grammar leads to more promotions and higher salaries. Write with Grammarly

C# Error CS0128 – A local variable named ‘variable’ is already defined in this scope

C# Compiler Error

CS0128 – A local variable named ‘variable’ is already defined in this scope

Reason for the Error

Have you ever declared two variables with the same name and that too with-in the same scope?. That’s when you will see this error.

Take a look at the below example.

namespace DeveloperPublishNamespace
{
    public class DeveloperPublish
    {
        public static void Main()
        {
            int variable1 = 0;
            char variable1 = 'c';
        }
    }
}

You have two variables with the same name (“variable1”) defined with-in the same scope (Main method). C# compiler will detect this and display the error CS0128 mentioning the same.

Error CS0128 A local variable or function named ‘variable1’ is already defined in this scope ConsoleApp1 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp1\Program.cs

C# Error CS0128 – A local variable named 'variable' is already defined in this scope

Solution

To fix the compiler error CS0128, ensure that you don’t use the same variable name more than once with-in the same scope.