Open In App

Deconstruction and Tuple Patterns in C#

Last Updated : 21 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C# 7.0 and later versions introduced deconstruction and tuple patterns, which simplify working with tuples, objects and complex data structures. These features make code more readable, concise and expressive by allowing multiple values to be extracted at once or matched in patterns.

Deconstruction

Deconstruction allows breaking down an object or tuple into its individual components using a single assignment statement. This is particularly useful with tuples or custom types that provide a Deconstruct method.

Example: Tuple Deconstruction

C#
var point = (X: 10, Y: 20);

// Deconstructing tuple into separate variables
var (x, y) = point;

Console.WriteLine($"X = {x}, Y = {y}");

Output:

X = 10, Y = 20

Deconstruction with Classes or Structs

For a class or struct, define a Deconstruct method to enable deconstruction.

C#
class Person
{
    public string Name { get; }
    public int Age { get; }

    public Person(string name, int age) => (Name, Age) = (name, age);

    public void Deconstruct(out string name, out int age) => (name, age) = (Name, Age);
}

var person = new Person("Alice", 30);
var (name, age) = person;

Console.WriteLine($"Name: {name}, Age: {age}");

Output:

Name: Alice, Age: 30

  • Deconstruction works with tuples and user-defined types.
  • Multiple variables can be initialized in a single statement.
  • Improves readability and reduces boilerplate code.

Tuple Patterns

Tuple patterns, introduced in C# 8.0 and enhanced in C# 9.0+, allow matching tuples directly in switch expressions or pattern matching statements. This is helpful for performing actions based on the combination of tuple elements.

Example: Tuple Pattern in Switch Expression

C#
var point = (X: 1, Y: 2);

string quadrant = point switch
{
    (0, 0) => "Origin",
    (var x, 0) => "X-Axis",
    (0, var y) => "Y-Axis",
    (var x, var y) when x > 0 && y > 0 => "Quadrant I",
    (var x, var y) when x < 0 && y > 0 => "Quadrant II",
    (var x, var y) when x < 0 && y < 0 => "Quadrant III",
    (var x, var y) when x > 0 && y < 0 => "Quadrant IV",
    _ => "Unknown"
};

Console.WriteLine(quadrant);

Output:

Quadrant I

  • Tuple patterns allow checking multiple values at once.
  • Can be combined with when clauses for conditional matching.
  • Useful in switch expressions and pattern matching scenarios.

Advanced Tuple Pattern Example

You can combine tuple patterns with deconstruction for compact code:

C#
(int, int)[] points = { (1, 2), (-1, 2), (-2, -3) };

foreach (var point in points)
{
    var quadrant = point switch
    {
        (var x, var y) when x > 0 && y > 0 => "Quadrant I",
        (var x, var y) when x < 0 && y > 0 => "Quadrant II",
        (var x, var y) when x < 0 && y < 0 => "Quadrant III",
        (var x, var y) when x > 0 && y < 0 => "Quadrant IV",
        _ => "On axis"
    };
    Console.WriteLine($"Point {point} is in {quadrant}");
}

Output:

Point (1, 2) is in Quadrant I

Point (-1, 2) is in Quadrant II

Point (-2, -3) is in Quadrant III

Benefits of Deconstruction and Tuple Patterns

  • Reduces boilerplate code for extracting multiple values.
  • Enhances readability and maintainability of code.
  • Supports concise switch expressions with tuple patterns.
  • Works with tuples, arrays and user-defined types.
  • Integrates seamlessly with modern C# pattern matching features.
Suggested Quiz
3 Questions

What is the main purpose of deconstruction in C#?

  • A

    To inherit multiple classes simultaneously

  • B

    To break down an object or tuple into individual components in a single statement

  • C

    To perform arithmetic operations on tuples

  • D

    To convert reference types into value types

Explanation:


Which method must a class or struct provide to support deconstruction?

  • A

    ToString()

  • B

    Deconstruct(out …)

  • C

    Equals()

  • D

    Clone()

Explanation:

A Deconstruct method with out parameters is required for a class or struct to support deconstruction into separate variables.

What is the advantage of using tuple patterns in switch expressions?

  • A

    They allow inheritance from multiple tuples

  • B

    They allow performing actions based on multiple tuple elements simultaneously

  • C

    They convert tuples into arrays automatically

  • D

    They prevent runtime exceptions in all cases

Explanation:

Tuple patterns enable matching multiple values at once in a switch expression or pattern matching, often combined with when clauses for conditional logic.

Image
Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore