Share via

Deconstruct in C#

Pip 265 Reputation points
2026-02-26T18:49:16.65+00:00

Hi,

I'm using .NET 10 with Visual Studio 2026.

Can you please explain this topic , how it used in many parts of C# languages?

Thanks in advance,

For example:

public readonly struct Point
{
    public int X { get; }
    public int Y { get; }

    public Point(int x, int y) => (X, Y) = (x, y);

    public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
}

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2026-02-26T18:49:32.5466667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Deconstruction in C# is syntax that lets an expression be “broken apart” into separate variables in a single operation. It is used with tuples, records, and any user-defined type that provides a suitable Deconstruct method.

    Using the Point example:

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

    Because Point defines:

    public void Deconstruct(out int x, out int y)
    

    it can be used like this:

    var p = new Point(3, 4);
    var (x, y) = p;   // deconstruction
    // x == 3, y == 4
    

    The compiler translates var (x, y) = p; roughly into:

    int x, y;
    p.Deconstruct(out x, out y);
    

    So deconstruction is just syntactic sugar over calling a Deconstruct method (or using tuple elements) and assigning the results to variables.

    Key concepts and where deconstruction is used:

    1. Tuples
      • For a tuple:
             (int a, int b) t = (1, 2);
             var (x, y) = t;   // x = 1, y = 2
        
      • The language knows how to deconstruct tuple types directly.
    2. User-defined types (class, struct, record)
      • Any type can support deconstruction by defining one or more Deconstruct methods:
             public void Deconstruct(out string fname, out string lname)
        
      • Then:
             var (first, last) = person;
        
      • Multiple overloads are allowed as long as they differ in number of parameters (arity). Overloads with the same number of parameters are ambiguous and not allowed.
    3. Extension Deconstruct methods
      • If a type cannot be modified, deconstruction can be added via extension methods:
             public static void Deconstruct(this SomeType value, out int a, out int b) { ... }
        
      • Then instances of SomeType can be deconstructed with var (a, b) = someInstance;.
    4. Records
      • For record types with a primary constructor, the compiler automatically synthesizes a Deconstruct method with out parameters for each positional parameter:
             public record House(int SquareFeet, string Address);
             
             var house = new House(1000, "123 Coder St.");
             var (sqft, addr) = house;  // uses synthesized Deconstruct
        
      • Only positional properties are included in the synthesized Deconstruct.
      • A custom Deconstruct can be added to change what gets deconstructed or to provide additional shapes.
    5. Discards
      • Unneeded values can be ignored using _:
             var (first, _, city, _) = person;
        
    6. How the compiler chooses what to call
      • When the left side is a tuple-like pattern, e.g. (a, b) = E;, the compiler deconstructs E as follows:
        1. If E is already a tuple expression with n elements, use its elements.
        2. Else if E has tuple type (T1, …, Tn), use its Item1Itemn.
        3. Else if E.Deconstruct(out …) resolves to a unique instance or extension method with n out parameters, call that method.
        4. Otherwise, E cannot be deconstructed.
    7. Use cases
      • Returning multiple values from a method in a readable way:
             (int min, int max) GetRange(...) => (...);
             
             var (min, max) = GetRange(...);
        
      • Extracting multiple properties from an object in one statement instead of multiple property accesses.
      • Pattern matching and switch expressions often combine with deconstruction for concise code.

    In summary, deconstruction is a language feature that:

    • Uses tuple syntax on the left-hand side of an assignment.
    • Is backed by tuples or Deconstruct(out ...) methods (instance or extension).
    • Is built into tuples and records and can be added to any user-defined type, as in the Point example.

    References: