Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Deconstruct methods

Reference for C# deconstruction: writing Deconstruct methods and extensions, tuple deconstruction, discards, overloads and use in patterns and foreach.

Deconstruction splits a value into several variables in one statement: var (x, y) = point;. Tuples support it out of the box. For your own types you add a method called Deconstruct whose parameters are all out parameters; the compiler calls it when it sees a deconstructing assignment. Positional records get a Deconstruct method generated for them.

The method can be an instance method or an extension method, so you can add deconstruction to types you do not own. It also powers positional patterns, so point is (0, var y) works for any type that has a suitable Deconstruct.

C#
public void Deconstruct(out int x, out int y) { x = X; y = Y; }
var (a, b) = point;

Try it

C# Your own Deconstruct

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Extension Deconstruct, tuples, foreach and swapping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
// instance method
public void Deconstruct(out T1 a, out T2 b, ...);

// extension method
public static void Deconstruct(this Type value, out T1 a, out T2 b);

// use
var (a, b) = value;
(T1 a, T2 b) = value;
(a, b) = value;                // existing variables
var (a, _) = value;            // discard
Rule Description
Name Exactly Deconstruct.
Return type void (the result is delivered through out parameters).
Parameters Two or more out parameters, in the order you want them assigned.
Overloads Allowed if they have different numbers of parameters. The compiler picks by the count on the left.

Errors: if no Deconstruct has the matching parameter count, the compiler reports a "cannot deconstruct" error. A one-element deconstruction is not allowed.

Usage notes

  • Every out value is assigned, even if you discard it with _. Keep Deconstruct cheap and free of side effects.
  • Overloads with the same number of parameters are ambiguous. Two Deconstruct(out int, out int) methods on a type cannot both exist; use different counts or names.
  • Instance methods win over extension methods when both match.
  • Tuple deconstruction needs no Deconstruct. ValueTuple supports it natively, and (a, b) = (b, a) swaps without a temporary variable.
  • Records: positional records generate Deconstruct for the primary-constructor parameters, in order. A with-style copy and deconstruction are independent features.
  • Deconstructing in foreach works for any element type that supports it: foreach (var (key, value) in dictionary).
  • Patterns: a positional pattern (var x, 0) calls Deconstruct; see Property and positional patterns.
  • Types do not need to match exactly. With explicit types on the left, the out values are converted implicitly: (double a, double b) = intPoint works if int converts to double.
  • Discards (_) are not variables and cannot be read. If a variable named _ already exists in scope, the meaning changes to assigning to it, so avoid naming variables _.
  • Not for large types. Past three or four elements the call sites become hard to read; use properties.