Skip to main content

How to Resolve Error "CS0135: 'declaration1' conflicts with the declaration 'declaration2'" in C#

The Compiler Error CS0135 is a scoping and naming error. The message reads: " 'Name' conflicts with the declaration 'Name' ".

C# enforces a strict rule regarding Simple Names: within a specific scope (like a method body), a specific identifier (name) must refer to the same entity throughout that entire block. You cannot use a name to refer to a class-level field in line 1, and then declare a local variable with the same name in line 5. This creates a contradiction in the meaning of that name within the method.

This guide explains this "Semantic Consistency" rule and how to fix the conflict.

Understanding the "One Meaning" Rule

In C#, a method body is considered a single scope. The compiler scans this scope to determine what every name refers to.

  • If you declare int x inside the method, the compiler decides: "In this method, x refers to this local variable."
  • If you use x (referring to a field this.x) before declaring the local x, the compiler gets confused. It says: "You used x on line 1 to mean the Field, but you declared x on line 5 to mean a Local Variable. A name cannot mean two different things in the same block."

Scenario 1: Field Usage Followed by Local Declaration

This is the most common trigger. You use a variable thinking it refers to a class property/field, but later in the same method, you declare a new variable with the same name.

Example of error:

public class ScoreKeeper
{
// Class-level field
public int count = 100;

public void UpdateScore()
{
// 1. Here, 'count' implicitly refers to 'this.count' (100).
System.Console.WriteLine(count);

// ... some lines of code later ...

// 2. Now we declare a local variable named 'count'.
// ⛔️ Error CS0135: 'count' conflicts with the declaration 'count'
// The compiler argues: "You can't define 'count' locally here, because
// you already used the name 'count' above to refer to something else."
int count = 50;

System.Console.WriteLine(count);
}
}

Scenario 2: Parameter Name Conflicts

This error can also occur in complex nesting or lambda expressions where a variable declared in an outer block is redeclared or used inconsistently in an inner block, creating ambiguity about which declaration "owns" the name.

While similar to CS0136 (Local variable hides surrounding variable), CS0135 specifically highlights when the definition itself is the source of the conflict because the name has been "polluted" by previous usage.

Example of error:

public class Program
{
public static void Main()
{
int i = 10;

// A separate block
{
// ⛔️ Error CS0135: In some C# versions/contexts, redeclaring 'i'
// if 'i' was used in a parent scope in a specific way causes conflict.
// (Though usually this specific simple case triggers CS0136).
// The nuance for CS0135 often involves 'using' statements or pattern matching.
int i = 20;
}
}
}

Solution: Rename or Qualify

The best solution is always to ensure local variables have unique, descriptive names that do not clash with class-level fields.

This removes all ambiguity. count refers to the field, newCount refers to the local calculation.

public class ScoreKeeper
{
public int count = 100;

public void UpdateScore()
{
// Refers to field
System.Console.WriteLine(count);

// ✅ Correct: Distinct name
int newCount = 50;
System.Console.WriteLine(newCount);
}
}

Fix 2: Use this. Qualifier

If you absolutely must use the same name (shadowing), you must be explicit about the first usage. However, you typically still cannot declare the local variable after the implicit usage. You generally have to move the declaration to the top or use this consistently.

note

Even using this.count might not satisfy the compiler if you declare int count later in the same scope, because the simple name count is still reserved by the local declaration.

The only robust fix for CS0135 is renaming.

public class ScoreKeeper
{
public int count = 100;

public void UpdateScore()
{
// Even if we use 'this.count', declaring 'int count' later
// makes the simple name 'count' ambiguous for the compiler in this scope.

// ✅ Ideally: Just don't reuse the name.
int localCount = 50;
}
}
note

Rule of Thumb: Avoid giving local variables the same names as your class fields (_count vs count is a popular naming convention to avoid this).

Conclusion

CS0135 ensures that code is readable and deterministic.

  1. Check Usage Order: Did you use a variable name, and then declare it later in the same function?
  2. Check Scope: Does that name refer to a field in the first usage and a local var in the second?
  3. Rename: Change the name of the local variable to be unique. This resolves the conflict immediately.