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.

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