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 Pattern Match Ignoring a Custom Deconstruct Order

- 24.11.26 - ErcanOPAK

🔧 C#: Fix a Pattern Match Ignoring a Custom Deconstruct Order

Positional pattern matching resolves to whichever public Deconstruct method on a type has a matching parameter count, and it matches pattern positions against that method’s out parameters strictly by position, never by name, so if a Deconstruct method’s parameter order doesn’t match the order a developer intuitively expects from the type’s property declaration order, a positional pattern can silently bind to the wrong values with no compiler error, since every position in the pattern is still assigned something, just not necessarily the thing the reader expected from that position.

🔎 The Problem

public readonly struct Point
{
    public int X { get; }
    public int Y { get; }
    public Point(int x, int y) { X = x; Y = y; }

    // Deliberately reversed for an internal serialization need -
    // but every positional pattern match elsewhere in the codebase
    // now silently receives (Y, X) instead of the (X, Y) order its
    // author assumed from the property declaration order above
    public void Deconstruct(out int y, out int x) { y = Y; x = X; }
}

// if (point is (var x, var y) && x > 100) ...
// `x` here is actually Y's value - compiles cleanly, no warning

✅ Fix: Keep Deconstruct Order Honest, or Match by Name

  • Name a Deconstruct method’s out parameters to exactly match the property names in the same order they’re declared, specifically so a positional pattern’s left-to-right reading order can never silently diverge from the type’s own declared property order.
  • When a Deconstruct method’s parameter order must differ from property declaration order for an unavoidable reason, add an XML doc comment directly on Deconstruct stating the actual order, since nothing in a positional pattern’s call site can otherwise reveal the mismatch.
  • Prefer named property patterns such as is { X: > 100 } over positional patterns for any type whose Deconstruct order isn’t immediately obvious from its declaration, since named patterns bind by property name and are immune to this entire class of ordering mistake.

🚨 Why This Is Dangerous

  • A positional pattern that silently binds values in the wrong order produces no error and no warning anywhere, so the bug surfaces only as logic that’s subtly wrong in a way that can look like a totally unrelated calculation error far from the actual mismatched Deconstruct.

A positional pattern trusts Deconstruct’s parameter order completely — the moment that order doesn’t match what you assumed from the type’s properties, the pattern is still matching something, just not what you think.

— C# Best Practice

Related posts:

C#: Fix WithCancellation on an IAsyncEnumerable Being Silently Ignored Because the Iterator's Token ...

C#: Fix WithCancellation on an IAsyncEnumerable Being Silently Ignored Because the Iterator's Token ...

C#: Use Records for Immutable Data Types

C#: Use Records for Immutable Data Types

C#: Use Static Members for Class-Level Data

C#: Use Static Members for Class-Level Data

Views: 9
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Ajax: Fix a Retry Wrapper Duplicating a Non-Idempotent POST
C#: Fix a Ref Struct Escaping Scope Through a Local Function

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 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 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# 868 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