0Pricing
C# Academy · Lesson

Deconstruction

Unpack tuples into separate variables.

What Is Deconstruction?

Deconstruction splits a tuple (or object) into separate variables in one statement, so you can name each part directly.

using System;

class Program
{
    static void Main()
    {
        var point = (3, 4);
        (int x, int y) = point;
        Console.WriteLine("x=" + x + ", y=" + y);
    }
}

Deconstructing a Tuple

Put the target variables in parentheses on the left of =. Each receives the matching tuple element.

using System;

class Program
{
    static void Main()
    {
        var user = ("Ada", 36);
        (string name, int age) = user;
        Console.WriteLine(name + " is " + age);
    }
}

All lessons in this course

  1. Value Tuples Basics
  2. Named Tuple Elements
  3. Deconstruction
  4. Tuples as Method Return Values
← Back to C# Academy