Skip to main content

How to Resolve Error "CS0128: A local variable named 'variable' is already defined in this scope" in C#

The Compiler Error CS0128 is a Scope and Naming error. The message reads: "A local variable named 'variable' is already defined in this scope".

In C#, every variable exists within a specific "scope" (usually defined by the curly braces { } of a method, loop, or conditional block). The language enforces a strict rule: You cannot have two local variables with the same name in the same scope, nor can you declare a variable inside a nested block (like an if statement) that has the same name as a variable in the outer block.

This guide explains the rules of variable scoping and how to resolve naming collisions.

Understanding Scope and Declaration

To the compiler, Declaring a variable and Assigning a variable are different actions.

  • Declaration: int x; (Tells the compiler "Reserve the name 'x' for an integer").
  • Assignment: x = 5; (Puts value 5 into 'x').

CS0128 occurs when you try to Declare the same name twice.

note

Scope Rule: A variable declared in a method is visible to the entire method, including any child blocks (if, for, while) inside it. Therefore, child blocks cannot reuse that name.

Scenario 1: Duplicate Declaration in the Same Block

The most basic cause is accidentally typing the data type (e.g., int, string, var) twice for the same variable name.

Example of error

public void Calculate()
{
int score = 10;

// ... some code ...

// ⛔️ Error CS0128: A local variable named 'score' is already defined in this scope.
// By writing 'int score' again, you are trying to create a NEW variable.
int score = 20;
}

Solution: Reassignment

If you want to update the value, remove the type declaration.

public void Calculate()
{
int score = 10;

// ✅ Correct: Reassign the EXISTING variable.
score = 20;
}

Scenario 2: Nested Block Shadowing (The "C++ Trap")

In languages like C++, you are allowed to declare a variable inside an if block that has the same name as a variable outside it (Shadowing). C# forbids this. If a variable i exists in the outer method, you cannot declare a new i inside a loop or condition within that method.

Example of error

public void ProcessData(bool reset)
{
int count = 100; // Outer scope

if (reset)
{
// ⛔️ Error CS0128: 'count' is already defined in the parent scope.
// You cannot "hide" the outer 'count' with a new inner 'count'.
int count = 0;
Console.WriteLine(count);
}
}

Solution: Rename or Reuse

Option A: Reuse If you meant to change the outer variable, remove the int keyword.

if (reset)
{
// ✅ Correct: Updates the outer variable
count = 0;
}

Option B: Rename If the inner variable is logically distinct, give it a unique name.

if (reset)
{
// ✅ Correct: 'tempCount' does not collide
int tempCount = 0;
Console.WriteLine(tempCount);
}

Scenario 3: Pattern Matching and 'out' Variables

Modern C# features like Pattern Matching (is Type varName) and Out Variables (out int varName) declare variables inline. These declarations spill into the surrounding scope and can collide with existing names.

Example of error

public void CheckInput(object obj)
{
string data = "Default";

// ⛔️ Error CS0128: 'data' is already defined above.
// The 'is string data' pattern tries to declare a new variable named 'data'.
if (obj is string data)
{
Console.WriteLine(data);
}
}

Solution

Choose a different name for the pattern variable.

public void CheckInput(object obj)
{
string data = "Default";

// ✅ Correct: 'str' is a unique name for the casted result
if (obj is string str)
{
Console.WriteLine(str);
}
}

Conclusion

CS0128 ensures that a variable name is unambiguous within a function.

  1. Check Assignments: Did you accidentally put a type (int, var) in front of a variable you are just trying to update?
  2. Check Nested Blocks: Are you trying to declare a variable inside an if or for loop that already exists outside of it? Rename the inner one.
  3. Check Patterns: Ensure your is patterns and out parameters use unique names.