Positional Patterns & Deconstruct Method Architecture
1Concept
Classes and structs implementing `public void Deconstruct(out T1 a, out T2 b)` enable positional pattern matching in switch expressions and tuple assignments.
2Architecture Diagram
Order Instance (Id: 501, Total: 1500.0, IsVip: true)
│
▼
[ Deconstruct(out total, out isVip) ]
│
▼
Positional Match: ( > 1000, true ) ──> "Apply 20% Enterprise VIP Discount" 3Code Example
C# 13 & .NET 9
using System;
public class Order
{
public int OrderId { get; set; }
public decimal Total { get; set; }
public bool IsVip { get; set; }
public void Deconstruct(out decimal total, out bool isVip)
{
total = Total;
isVip = IsVip;
}
}
public class PositionalPatternDemo
{
public static void Main()
{
var order = new Order { OrderId = 99, Total = 2500m, IsVip = true };
string status = order switch
{
( > 2000m, true) => "Executive VIP Priority Delivery",
( > 1000m, _) => "Standard High-Value Order",
_ => "Standard Processing"
};
Console.WriteLine($"Order Status: {status}");
}
}4Expected Output
Order Status: Executive VIP Priority Delivery
5Key Takeaways
- ✓Records auto-generate `Deconstruct` methods for primary constructor parameters.
- ✓Deconstruct patterns enable clean domain-driven status evaluation.
- ✓Multiple `Deconstruct` overloads with different arities can coexist.