Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Fix a Deconstruct Method Never Matching a Pattern Switch

- 18.11.26 - ErcanOPAK

đź”§ C#: Fix a Deconstruct Method Never Matching a Pattern Switch

Positional pattern matching in a switch resolves against a type’s Deconstruct method using ordinary overload and extension-method lookup rules, which means a Deconstruct defined as an extension method only works for pattern matching where that extension’s namespace is actually in scope via a using directive. Leave the using out at the point the pattern appears, and the pattern doesn’t error — it simply never matches that shape, falling through to the next case or the default with no indication that the real problem is a missing import.

🔎 The Problem

namespace Geometry.Extensions
{
    public static class PointExtensions
    {
        public static void Deconstruct(this Point p, out int x, out int y)
            => (x, y) = (p.X, p.Y);
    }
}

// In a file WITHOUT "using Geometry.Extensions;":
string Describe(Point p) => p switch
{
    (0, 0) => "origin",      // never matches - Deconstruct not in scope
    _ => "elsewhere"         // every point, including (0,0), lands here
};

âś… Fix: Keep Deconstruct Discoverable Where the Pattern Is Used

  • Add the exact using directive for the extension method’s namespace in every file that uses a positional pattern depending on it — the pattern won’t error without it, so this has to be checked deliberately, not left to the compiler to flag.
  • Prefer an instance Deconstruct method over an extension-method one when a type’s values need to be reliably pattern-matchable, since instance members don’t carry the same scope-dependent risk.
  • Write a unit test that exercises the pattern match directly with a known-matching value and asserts the expected branch was taken, rather than trusting that code which compiles cleanly must also be matching correctly.

🚨 Why This Is Dangerous

  • A pattern that silently never matches doesn’t just fail once — it fails identically for every input from that point forward, consistently routing every case to the fallback branch in a way that can look like intentional, working default behavior rather than a bug.

A missing using directive doesn’t always cost you a compile error — sometimes it just quietly costs you the one case your switch was written to catch.

— C# Best Practice

Related posts:

C# “Enum Flags” — The Powerful Feature 90% Underuse

C# “Enum Flags” — The Powerful Feature 90% Underuse

C#: Use List Patterns for Powerful Array and List Matching

C#: Use List Patterns for Powerful Array and List Matching

C#: Master System.Threading.Channels for Producer-Consumer Patterns

C#: Master System.Threading.Channels for Producer-Consumer Patterns

Views: 2

Post navigation

SQL Server: Fix a Temp Table Causing a Plan Recompile Every Call
Visual Studio: Fix IntelliCode Suggestions Stuck on an Old Snippet

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

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