Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
C#

C#: Fix a Switch Expression Missing an Exhaustiveness Warning

- 29.11.26 - ErcanOPAK

🔧 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.

— C# Best Practice

Related posts:

C# Collections Resize Too Often

C# Collections Resize Too Often

C#: Use Task-Based Asynchronous Pattern (TAP)

C#: Use Task-Based Asynchronous Pattern (TAP)

C#: Fix a Span Capture Throwing in an Async Local Function

C#: Fix a Span Capture Throwing in an Async Local Function

Views: 3
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Kubernetes: Fix a Liveness Probe Restarting a Pod Mid-Migration
The AI Prompt That Audits a Cron Job for Missing Timezone Handling

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com