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



