Skip to main content

How to Resolve Error "CS0152: The label 'label' already occurs in this switch statement" in C#

The Compiler Error CS0152 is a logical ambiguity error. The message reads: "The label 'case X:' already occurs in this switch statement".

In a switch statement, every case label acts as a distinct entry point for the control flow. When the switch evaluates the expression, it must know exactly which single block of code to execute. If you define two case labels with the same constant value, the compiler cannot decide which one to pick, resulting in this error.

This guide explains how duplicate labels occur and how to merge logic correctly.

Understanding Switch Uniqueness

A switch statement is essentially a jump table.

  • If x is 1, jump to address A.
  • If x is 2, jump to address B.

If you say:

  • If x is 1, jump to address A.
  • If x is 1, jump to address C.

The compiler encounters a conflict. It is impossible for the variable to match the same value in two different logical branches simultaneously.

Scenario 1: Duplicate Literals (Copy-Paste)

The most basic cause is accidentally typing the same number or string twice, often due to copying a case block and forgetting to update the value.

Example of error

public void ProcessKey(char key)
{
switch (key)
{
case 'A':
Console.WriteLine("Move Left");
break;

// ... many lines of code ...

// ⛔️ Error CS0152: The label 'case 'A':' already occurs in this switch statement
case 'A':
Console.WriteLine("Attack");
break;
}
}

Solution: Correct the Value

Change the duplicate label to the intended unique value.

public void ProcessKey(char key)
{
switch (key)
{
case 'A':
Console.WriteLine("Move Left");
break;

// ✅ Correct: Changed 'A' to 'B'
case 'B':
Console.WriteLine("Attack");
break;
}
}

Scenario 2: Duplicate Constants or Enums

This scenario is trickier because the names of the labels look different, but their values are the same.

The switch statement evaluates the underlying value. If you have two constants defined as 5, using both as case labels will trigger CS0152.

Example of error

public class Config
{
public const int ModeStart = 1;
public const int ModeResume = 1; // Mistake: Duplicate value
}

public class Game
{
public void Run(int mode)
{
switch (mode)
{
case Config.ModeStart:
Console.WriteLine("Starting...");
break;

// ⛔️ Error CS0152: Even though 'ModeResume' is a different name,
// its value is '1', which is already handled by 'ModeStart'.
case Config.ModeResume:
Console.WriteLine("Resuming...");
break;
}
}
}

Solution: Fix the Constants

Ensure your constants or Enum values are distinct.

public class Config
{
public const int ModeStart = 1;
// ✅ Correct: Unique value
public const int ModeResume = 2;
}

Solution: Merging Cases (Fall-Through)

Often, developers create duplicates because they want two different inputs to execute the same code.

Incorrect:

case 1: DoWork(); break;
case 1: DoWork(); break; // Error

Correct: You can stack case labels on top of each other. This is called "fall-through" (or stacking).

public void CheckStatus(int code)
{
switch (code)
{
// ✅ Correct: Both 200 and 201 will execute the same block
case 200:
case 201:
Console.WriteLine("Success");
break;

case 404:
Console.WriteLine("Not Found");
break;
}
}
tip

Pattern Matching (C# 7.0+): You can also use logical operators in patterns: case 200 or 201: ... break;

Conclusion

CS0152 ensures that every path in a switch statement is unique.

  1. Check Literals: Did you copy-paste case 1: twice?
  2. Check Constants: Do ConstA and ConstB hold the same underlying number?
  3. Stack Labels: If you want multiple cases to do the same thing, list them one after another (case A: case B: ...) instead of creating duplicate blocks.