C# Academy · Lesson

Deconstruction

Unpack tuples into separate variables.

Lesson 3 of 413 steps

Deconstruction is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

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);
    }
}

Using var in Deconstruction

You can let the compiler infer types with var, either per variable or for the whole group.

using System;

class Program
{
    static void Main()
    {
        var data = (1, "one", true);
        var (id, word, flag) = data;
        Console.WriteLine(id + " " + word + " " + flag);
    }
}

Deconstructing a Method Result

You can deconstruct the tuple a method returns directly into named variables.

using System;

class Program
{
    static (int, int) MinMax(int a, int b) => a < b ? (a, b) : (b, a);

    static void Main()
    {
        var (lo, hi) = MinMax(8, 3);
        Console.WriteLine("min=" + lo + ", max=" + hi);
    }
}

Discarding Values

Use an underscore _ to ignore parts you do not need during deconstruction.

using System;

class Program
{
    static (int, int, int) Rgb() => (255, 128, 0);

    static void Main()
    {
        var (r, _, _) = Rgb();
        Console.WriteLine("Red channel: " + r);
    }
}

Deconstructing into Existing Variables

You can assign to already-declared variables by leaving out the type names.

using System;

class Program
{
    static void Main()
    {
        int a = 0, b = 0;
        (a, b) = (10, 20);
        Console.WriteLine(a + ", " + b);
    }
}

Swapping with Tuples

A neat trick: swap two variables in one line using tuple deconstruction, no temp variable needed.

using System;

class Program
{
    static void Main()
    {
        int x = 1, y = 2;
        (x, y) = (y, x);
        Console.WriteLine("x=" + x + ", y=" + y);
    }
}

The Deconstruct Method

Your own classes can support deconstruction by defining a Deconstruct method with out parameters.

using System;

class Point
{
    public int X, Y;
    public Point(int x, int y) { X = x; Y = y; }

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

class Program
{
    static void Main()
    {
        var p = new Point(5, 9);
        var (px, py) = p;
        Console.WriteLine(px + ", " + py);
    }
}

Multiple Deconstruct Overloads

A class can offer several Deconstruct overloads exposing different numbers of values.

using System;

class Person
{
    public string Name; public int Age; public string City;
    public Person(string n, int a, string c) { Name = n; Age = a; City = c; }

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

class Program
{
    static void Main()
    {
        var p = new Person("Ada", 36, "London");
        var (name, age) = p;
        Console.WriteLine(name + ", " + age);
    }
}

Deconstruction in foreach

When looping over a collection of tuples, you can deconstruct each element right in the foreach header.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var pairs = new List<(string, int)>
        {
            ("Ann", 30),
            ("Bob", 25)
        };
        foreach (var (name, age) in pairs)
            Console.WriteLine(name + " is " + age);
    }
}

Putting It Together

Deconstruction makes code that returns or stores multiple values read cleanly, naming each part at the point of use.

using System;

class Program
{
    static (int Quotient, int Remainder) Divide(int a, int b) => (a / b, a % b);

    static void Main()
    {
        var (q, r) = Divide(17, 5);
        Console.WriteLine("17 / 5 = " + q + " remainder " + r);
    }
}

Quick Check

Test your understanding of deconstruction.

Recap

Deconstruction splits a tuple or object into named variables in one statement. Use var (a, b) = ..., discard parts with _, swap variables, and deconstruct in foreach. Custom classes opt in with a Deconstruct method using out parameters, optionally overloaded.

using System;

class Program
{
    static void Main()
    {
        var (a, b, c) = (1, 2, 3);
        Console.WriteLine(a + b + c);
    }
}
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Deconstruction” lesson free?

Yes — the full text of “Deconstruction” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.

What will I learn in “Deconstruction”?

Unpack tuples into separate variables. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Deconstruction” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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