C# deconstruct
last modified November 15, 2026
This tutorial shows how to deconstruct tuples, dictionary entries, classes, structs, and records in C#, how to add deconstruction to existing types, and how to use deconstruction in pattern matching.
Deconstruction unpacks a value into separate variables in a single
statement. Tuples support it out of the box. Other types support it when they
have an accessible Deconstruct method, either an instance method
or an extension method, with one out parameter per variable.
Deconstructing tuples
In the first example we unpack tuples into variables.
var user = ("John", "Doe", "gardener");
var (firstName, lastName, occupation) = user;
Console.WriteLine(firstName);
Console.WriteLine(lastName);
Console.WriteLine(occupation);
Console.WriteLine("-----------------");
(string fn, string ln, string oc) = GetUser();
Console.WriteLine($"{fn} {ln}, {oc}");
(string, string, string) GetUser() => ("Roger", "Roe", "driver");
The user tuple has three items, which are unpacked into three variables.
var (firstName, lastName, occupation) = user;
With var outside the parentheses, the type of each variable is
inferred from the corresponding tuple item.
(string fn, string ln, string oc) = GetUser();
The types can also be written explicitly for each variable. Here we deconstruct the tuple returned by a local function. This is a common way to return several values from a method.
$ dotnet run John Doe gardener ----------------- Roger Roe, driver
Discards, swaps, and existing variables
Items that we do not need are skipped with the _ discard.
Deconstruction can also assign to variables that already exist.
var user = ("John", "Doe", "gardener");
var (fname, lname, _) = user;
Console.WriteLine($"{fname} {lname}");
var (_, _, job) = user;
Console.WriteLine(job);
int a = 1;
int b = 2;
(a, b) = (b, a);
Console.WriteLine($"a: {a}, b: {b}");
string name;
(name, var age) = ("Paul", 34);
Console.WriteLine($"{name} is {age}");
The example uses discards, swaps two variables, and mixes a new variable with an existing one.
var (fname, lname, _) = user;
The third item is discarded. We can use any number of discards.
(a, b) = (b, a);
Without var, deconstruction assigns to existing variables. Since
the right-hand tuple is built first, this swaps the values without a
temporary variable.
string name;
(name, var age) = ("Paul", 34);
Since C# 10, one deconstruction can both assign to an existing variable and declare a new one.
$ dotnet run John Doe gardener a: 2, b: 1 Paul is 34
Deconstructing in foreach loops
The elements of a collection can be deconstructed directly in a
foreach statement. Dictionary entries are
KeyValuePair values, which have a Deconstruct
method since .NET Core 2.0.
var domains = new Dictionary<string, string>
{
["sk"] = "Slovakia",
["ru"] = "Russia",
["de"] = "Germany",
["no"] = "Norway"
};
foreach (var (code, country) in domains)
{
Console.WriteLine($"{code}: {country}");
}
Console.WriteLine("-----------------");
List<(string Name, int Score)> results = [("Alice", 82), ("Bob", 75)];
foreach (var (name, score) in results)
{
Console.WriteLine($"{name}: {score}");
}
We loop over a dictionary and over a list of tuples.
foreach (var (code, country) in domains)
Each key-value pair is unpacked into the code and
country variables.
foreach (var (name, score) in results)
The elements of the list are named tuples. The variable names in the loop do not have to match the element names of the tuple.
$ dotnet run sk: Slovakia ru: Russia de: Germany no: Norway ----------------- Alice: 82 Bob: 75
Deconstructing classes
A class supports deconstruction when it defines a Deconstruct
method. The method returns void and has an out
parameter for each produced value. A class can have several overloads with
different numbers of parameters.
var u = new User("John", "Doe", "gardener");
var (fname, lname, occupation) = u;
Console.WriteLine($"{fname} {lname} is a {occupation}");
var (fn, ln) = u;
Console.WriteLine($"{fn} {ln}");
class User(string firstName, string lastName, string occupation)
{
public string FirstName { get; } = firstName;
public string LastName { get; } = lastName;
public string Occupation { get; } = occupation;
public void Deconstruct(out string fname, out string lname)
{
fname = FirstName;
lname = LastName;
}
public void Deconstruct(out string fname, out string lname,
out string occupation)
{
(fname, lname) = (FirstName, LastName);
occupation = Occupation;
}
}
The User class can be deconstructed into two or three variables.
var (fname, lname, occupation) = u;
The compiler picks the Deconstruct overload that has three
out parameters.
var (fn, ln) = u;
With two variables, the two-parameter overload is used. Overloads should
differ in the number of parameters; with two overloads of the same count, a
deconstruction with var fails with an ambiguity error (CS0121).
public void Deconstruct(out string fname, out string lname)
{
fname = FirstName;
lname = LastName;
}
The method assigns every out parameter. The compiler translates
var (fn, ln) = u; into a call
u.Deconstruct(out var fn, out var ln).
$ dotnet run John Doe is a gardener John Doe
Deconstructing structs and positional patterns
Structs are deconstructed the same way as classes. A type with a
Deconstruct method can also be used in positional
patterns, which match the deconstructed values.
var p = new Point(5, 10);
var (x, y) = p;
Console.WriteLine($"x: {x}, y: {y}");
Point[] points = [new(0, 0), new(3, 0), new(0, 7), new(2, 2)];
foreach (var pt in points)
{
string desc = pt switch
{
(0, 0) => "origin",
(_, 0) => "on the x axis",
(0, _) => "on the y axis",
var (px, py) when px == py => "on the diagonal",
_ => "elsewhere"
};
Console.WriteLine($"{pt}: {desc}");
}
readonly struct Point(int x, int y)
{
public int X { get; } = x;
public int Y { get; } = y;
public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
public override string ToString() => $"({X}, {Y})";
}
We define a Point struct and classify points with a switch
expression.
public void Deconstruct(out int x, out int y) => (x, y) = (X, Y);
The body of the method assigns both out parameters at once with a
tuple.
(0, 0) => "origin", (_, 0) => "on the x axis",
A positional pattern calls Deconstruct and matches each value
against a nested pattern: a constant, a discard, or another pattern.
var (px, py) when px == py => "on the diagonal",
A var pattern deconstructs the point into variables that the
when clause can use.
$ dotnet run x: 5, y: 10 (0, 0): origin (3, 0): on the x axis (0, 7): on the y axis (2, 2): on the diagonal
Deconstructing records
For a positional record, the compiler generates a Deconstruct
method with one parameter for each positional parameter. When a value is
itself deconstructible, deconstruction can be nested.
var user = new User("John", "Doe", new Address("Bratislava", "Slovakia"));
var (firstName, lastName, address) = user;
Console.WriteLine($"{firstName} {lastName} lives in {address.City}");
var (_, _, (city, country)) = user;
Console.WriteLine($"{city}, {country}");
if (user is (_, _, (_, "Slovakia")))
{
Console.WriteLine($"{firstName} lives in Slovakia");
}
record Address(string City, string Country);
record User(string FirstName, string LastName, Address Address);
A user record contains an address record.
var (firstName, lastName, address) = user;
The user is deconstructed into three variables; the last one holds the
Address record.
var (_, _, (city, country)) = user;
Nested parentheses deconstruct the address as well. The names are discarded.
if (user is (_, _, (_, "Slovakia")))
Positional patterns can be nested too. The pattern matches users whose
address has the country Slovakia.
$ dotnet run John Doe lives in Bratislava Bratislava, Slovakia John lives in Slovakia
Deconstruct extension methods
We can add deconstruction to types that we do not own with an extension
method. C# 14 also allows extension members declared in an
extension block.
var dt = new DateTime(2026, 11, 15);
(int year, int month, int day) = dt;
Console.WriteLine($"{year} {month} {day}");
var (hours, minutes) = TimeSpan.FromMinutes(135);
Console.WriteLine($"{hours}h {minutes}m");
public static class DateExtensions
{
public static void Deconstruct(this DateTime date, out int year,
out int month, out int day)
{
year = date.Year;
month = date.Month;
day = date.Day;
}
extension(TimeSpan span)
{
public void Deconstruct(out int hours, out int minutes)
{
hours = (int)span.TotalHours;
minutes = span.Minutes;
}
}
}
We deconstruct a DateTime into its date parts and a
TimeSpan into hours and minutes.
public static void Deconstruct(this DateTime date, out int year,
out int month, out int day)
A classic extension method: the this modifier on the first
parameter extends the DateTime type.
extension(TimeSpan span)
{
public void Deconstruct(out int hours, out int minutes)
A C# 14 extension block declares the receiver once; the members inside it are written like instance methods. Deconstruction finds both kinds of extensions.
$ dotnet run 2026 11 15 2h 15m
Source
Deconstructing tuples and other types
In this article we have worked with deconstruction in C#. See also the tuple, record, and KeyValuePair tutorials.
Author
List all C# tutorials.