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

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

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