How to deconstruct an object in C#

By FoxLearn 3/6/2025 3:19:59 AM   4
Deconstructing an object means assigning its properties to several variables in a single line of code using deconstruction assignment syntax (also known as destructuring or unpacking).

For example:

var product = new Product()
{
    Name = "Laptop",
    Price = 1200,
    InStock = true
};

var (productName, productPrice) = product;

Console.WriteLine($"The {productName} costs ${productPrice}");

Output:

The Laptop costs $1200

In C#, several built-in types already support deconstruction, such as tuples, dictionaries (KeyValuePair), and records. You can enable deconstruction for any type by implementing the Deconstruct() method.

Add the Deconstruct() Method

Here’s an example of adding the Deconstruct() method to a class:

public class Product
{
    public string Name { get; set; }
    public double Price { get; set; }
    public bool InStock { get; set; }

    public void Deconstruct(out string name, out double price)
    {
        name = Name;
        price = Price;
    }
}

In this example, the Deconstruct() method is defined using a few key conventions:

Once you've implemented Deconstruct(), you can use the deconstruction assignment syntax:

var (productName, productPrice) = product;

The deconstruction syntax is doing the following:

string productName;
double productPrice;
product.Deconstruct(out productName, out productPrice);

Multiple Deconstruct() Methods

You can implement multiple Deconstruct() methods to support deconstruction of different combinations of properties.

public class Product
{
    public string Name { get; set; }
    public double Price { get; set; }
    public bool InStock { get; set; }

    public void Deconstruct(out string name, out double price)
    {
        name = Name;
        price = Price;
    }

    public void Deconstruct(out string name, out double price, out bool inStock)
    {
        name = Name;
        price = Price;
        inStock = InStock;
    }
}

Now, you can use either of these deconstruct methods like so:

var (productName, productPrice) = product;
var (productName2, productPrice2, productStock) = product;

Error CS8130 – Type Inference Issues

If you have two Deconstruct() methods with the same number of parameters, the compiler might not be able to infer the types for the variables in the deconstruction assignment, causing error CS8130.

To resolve this, you can explicitly declare the types, like so:

(string productName, double productPrice) = product;

Add Deconstruct() as an Extension Method

If you want to enable deconstruction for types you don’t control, you can add the Deconstruct() method as an extension method.

public static class ProductExtensions
{
    public static void Deconstruct(this Product product, out string name, out double price)
    {
        name = product.Name;
        price = product.Price;
    }
}

Now, you can deconstruct any Product object like this:

var product = new Product()
{
    Name = "Smartphone",
    Price = 800,
    InStock = false
};

var (name, price) = product;

Even if a class already has a Deconstruct() method, you can create your own extension method to deconstruct exactly the properties you need.