Compiler error · error CS0128

A local variable named 'X' is already defined in this scope

You declared the same name twice in the same scope. In C#, declaring reserves the name — a second declaration collides, even if the types match.

See it happen

int score = 90;
Console.WriteLine(score);
int score = 85;     // CS0128: 'score' is already taken in this scope

Fix 1: you meant to ASSIGN, not re-declare

The most common form: copy-pasted a line that still carries its type. Assignment uses the bare name:

int score = 90;
Console.WriteLine(score);
score = 85;         // no 'int' — plain assignment
Console.WriteLine(score);

Rule: the type keyword (int, string, var…) appears ONCE per scope per name. Every later use is just the name.

Fix 2: you meant a different variable

int original = 90;
int revised = 85;               // genuinely two things: two names
Console.WriteLine($"original: {original}, revised: {revised}");

The confusing part: same name in DIFFERENT scopes is fine

Blocks create scopes. A loop counter declared inside the loop body is a new i each time — and a method parameter may share a name with a field:

for (int i = 0; i < 2; i++)
{
    int slot = i + 1;      // fresh 'slot' every iteration — no collision
    Console.WriteLine(slot);
}

for (int i = 0; i < 2; i++)   // a second loop CAN reuse 'i': different scope
{
    Console.WriteLine(i + 1);
}

class Dog
{
    public string Name = "field";

    public void Rename(string Name)     // parameter shadows the field — legal
    {
        Console.WriteLine("name param wins inside");
    }
}
new Dog().Rename("Rex");

CS0128 is only about one scope, one name. Nested scopes shadowing outer names is legal — but frowned upon, because readers lose track of which variable they're looking at. Prefer distinct names.

Frequently asked

What does error CS0128 mean in C#?

Yes — separate loops can each declare int i, and a loop body gets a fresh scope per iteration. CS0128 only fires for two declarations in the SAME scope.

Can two variables share a name in different scopes?

undefined

Checklist