🔧 Fix a Switch Expression Missing an Exhaustiveness Warning
The compiler only raises CS8509 — “the switch expression does not handle all possible values” — when it can actually see that a case is missing, and a bare discard pattern (_ => …) technically handles every value by definition, satisfying the compiler completely. A switch expression written with a catch-all discard arm specifically to silence that warning during initial development keeps compiling cleanly forever afterward, even once someone adds a brand-new member to the enum it switches over — the new value quietly falls into the discard arm instead of triggering the warning that exhaustiveness checking exists to give you.
🔎 The Problem
public enum OrderStatus { Pending, Shipped, Delivered }
string Describe(OrderStatus status) => status switch
{
OrderStatus.Pending => "Waiting to ship",
OrderStatus.Shipped => "On its way",
OrderStatus.Delivered => "Delivered",
_ => "Unknown", // added early on to silence CS8509
};
// Someone later adds OrderStatus.Cancelled - this switch compiles with
// zero warnings and silently returns "Unknown" for it, exactly as if
// that were the intended, reviewed behavior for the new status
✅ Fix: A Switch Expression Missing an Exhaustiveness Warning
- Replace a blanket `_ => “Unknown”` on an enum switch with `_ => throw new UnreachableException($”Unhandled {nameof(OrderStatus)}: {status}”)`, so a genuinely new enum value fails loudly at runtime instead of silently matching the fallback.
- Let CS8509 do its job: list every enum member explicitly without a discard arm wherever you can, so the compiler itself flags a missing case the moment a new member is added, rather than relying on anyone remembering to check.
- If you do need a real fallback behavior for unknown values (data coming from outside the compiled assembly, for instance), keep it, but make it log or assert loudly rather than returning a quiet placeholder string that looks identical to a reviewed case.
🚨 Why This Is Dangerous
- A new enum member that silently lands in a leftover discard arm can ship to production with completely wrong behavior for that case, and because the build is clean and the tests that exist still pass, nothing flags it until someone notices the output is wrong.
A discard arm satisfies the compiler’s exhaustiveness check once — and then quietly keeps satisfying it, forever, no matter what gets added to the enum afterward.



