Ad Space — Top Banner

CS0162

C# Programming Language

Severity: Minor

What Does This Error Mean?

CS0162 is a compiler warning that tells you some of your code can never execute. This happens when a return, break, throw, or goto statement unconditionally stops execution before the code that follows. The warning means something is logically wrong — either the code before is wrong, or the code after should be removed. This is a warning by default, not an error, but it usually signals a real logic bug.

Affected Models

  • .NET Framework
  • .NET Core
  • .NET 5+
  • Visual Studio
  • Visual Studio Code
  • Rider

Common Causes

  • A return statement appears before other code in the same block, making that later code impossible to reach
  • A throw statement unconditionally ends execution, but code follows it in the same block
  • A condition like 'if (true)' always runs, making the else block permanently unreachable
  • A break or continue inside a loop appears before more loop-body code, cutting it off
  • Code written after a goto or infinite loop that can never be jumped to

How to Fix It

  1. Find the line flagged by the warning. Look at the code immediately before it. There should be an unconditional return, throw, break, or similar statement stopping execution.

    The warning line is the unreachable code. The problem is the line(s) before it that unconditionally exit the block.

  2. Decide whether the unreachable code should be deleted (it was added by mistake) or whether the early exit should be conditional (wrapped in an if statement).

    If the code after the return was supposed to run sometimes, move the return inside an if block so it only exits under certain conditions.

  3. Check for conditions that are always true or always false. 'if (true)' or 'if (false)' are obvious mistakes. More subtle cases: 'if (x == x)' is always true.

    Review your boolean conditions carefully. A condition that is always the same value means one of your branches will never run.

  4. In switch statements, make sure each case either ends with break, return, or falls through intentionally. Code after a return inside a case is unreachable.

    This is a common pattern: writing extra code after a return in a switch case. Either remove it or move it before the return.

  5. If the unreachable code was leftover from a refactoring — old code that is no longer needed — delete it. Keep your codebase clean.

    Unreachable code adds confusion for anyone reading the code later, including yourself. If it cannot run, remove it.

When to Call a Professional

CS0162 is a warning you can fix yourself. It is not a crash — your program will still compile and run. However, unreachable code is almost always a sign of a logic mistake. Do not suppress the warning — investigate and fix the underlying problem.

Frequently Asked Questions

CS0162 is just a warning — can I ignore it?

Technically yes, but you should not. Unreachable code almost always means either a logical mistake or dead code that should be deleted. If the logic is wrong, ignoring the warning means the program will not behave as intended. Clean code has no warnings — treat warnings as errors to maintain code quality.

Why would a developer ever write unreachable code?

Usually by accident during development. Common scenarios: adding a return statement and forgetting to move or delete the code below it, leaving debug or placeholder code behind after restructuring, or writing a condition that seems variable but is actually always the same. Sometimes unreachable code appears after refactoring — old code paths that are no longer needed but were not fully removed.

Can I tell the compiler to stop showing this warning?

Yes — you can suppress it with '#pragma warning disable CS0162' before the code and '#pragma warning restore CS0162' after. However, suppressing warnings is generally bad practice. Before suppressing, ask yourself honestly: is this code genuinely needed, or is it dead code that should be deleted? Suppressing warnings hides problems instead of solving them.