Skip to main content

How to Resolve Warning "CS0429: Unreachable expression code detected" in C#

The Compiler Warning CS0429 is a logic warning related to Short-Circuit Evaluation. The message reads: "Unreachable expression code detected".

In C#, the logical operators && (AND) and || (OR) are short-circuiting. This means that if the result of the expression can be determined solely by the left-hand operand, the right-hand operand is never evaluated. If the left-hand operand is a compile-time constant (like true or false), the compiler knows that the code on the right side will absolutely never run.

This warning is crucial because if the right-hand side contains a method call with side effects (like saving data or logging), those actions will silently fail to happen.

Understanding Short-Circuiting

  • OR (A || B): If A is true, the whole expression is true. B is skipped.
  • AND (A && B): If A is false, the whole expression is false. B is skipped.

When A is a variable determined at runtime, this is a feature. When A is a constant (like const bool A = true;), the compiler warns you that B is dead code.

Scenario 1: Logical OR (||) with Constant True

This typically happens when a developer hardcodes a value to true for debugging purposes (to bypass a check) and forgets to remove the rest of the expression.

Example of error:

public class AuthSystem
{
private const bool BypassAuth = true;

public bool Login(string user, string password)
{
// ⛔️ Warning CS0429: Unreachable expression code detected
// Because 'BypassAuth' is CONSTANT true, the expression 'CheckDb(...)'
// is theoretically unreachable code.
if (BypassAuth || CheckDb(user, password))
{
return true;
}
return false;
}

private bool CheckDb(string u, string p)
{
Console.WriteLine("Checking Database...");
return true;
}
}

Result: "Checking Database..." is never printed. If CheckDb had important side effects (like logging a login attempt), those side effects are lost.

Scenario 2: Logical AND (&&) with Constant False

Similarly, if you disable a feature using a constant false flag, any logic chained with && after that flag will be unreachable.

Example of error:

public class FeatureManager
{
private const bool IsFeatureEnabled = false;

public void Initialize()
{
// ⛔️ Warning CS0429: Unreachable expression code detected
// The compiler sees 'false && ...'. It knows 'Setup()' will never run.
if (IsFeatureEnabled && Setup())
{
Console.WriteLine("Feature Ready");
}
}

private bool Setup()
{
// Complex initialization logic
return true;
}
}

Solution: Remove or Fix Constants

To resolve this, you must clarify your intent.

Option A: Remove the Dead Code (Clean Up)

If the constant is permanent (e.g., a feature toggle that is permanently off), remove the logic that will never run to keep the code clean.

public void Initialize()
{
// ✅ Correct: Code removed because IsFeatureEnabled is const false.
}

Option B: Make the Variable Runtime-Dependent

If the variable is meant to be configurable, remove the const keyword. Use static readonly or a standard property. The compiler cannot predict runtime values, so it will disable the short-circuit warning (though the code will still short-circuit at runtime).

public class AuthSystem
{
// ✅ Correct: Changed 'const' to 'static readonly' or a property.
// The compiler can no longer guarantee the value at compile-time,
// so it must generate code to evaluate the expression.
private static readonly bool BypassAuth = true;

public bool Login(string user, string password)
{
if (BypassAuth || CheckDb(user, password))
{
return true;
}
return false;
}
}

Option C: Use Bitwise Operators (Rare)

If you strictly need both sides to evaluate (e.g., you need the side effects of the method call) even if the first part determines the boolean result, use the single & or | operators. These are not short-circuiting.

// ✅ Correct: Both sides execute, even if BypassAuth is true.
// Note: This is rare in conditional logic and usually discouraged for bools
// unless side-effects are explicitly required.
if (BypassAuth | CheckDb(user, password))
{
}

Conclusion

CS0429 acts as a "Dead Code" detector for expressions.

  1. Check Constants: Look for const bool variables in your if statements.
  2. Verify Intent:
    • If you wanted to disable code temporarily, ignore the warning or comment out the code properly.
    • If you wanted a runtime configuration, change const to static readonly or a config lookup.
  3. Watch for Side Effects: Ensure you aren't skipping critical method calls (like logging or state updates) due to a hardcoded true/false.