NDepend Blog

Improve your .NET code quality with NDepend

Deconstruction in C#

April 24, 2024 2 minutes read

Deconstruction in C#

C# 7.0 introduced the deconstruction syntax. It allows developers to extract in a single expression, properties of an object or elements of a tuple and then to assign them to distinct variables. Here is a small program candidate to be simplified with deconstruction:

Here is the same example with deconstructing the object pat in the line var (name, birthDate) = pat; thanks to a new method name Deconstruct() :

In this revised example, we see that numerous lines of code can be conserved by extracting multiple values from an object into several local variables. Thanks to deconstruction this can be achieved in a single expression.

If we decompile this example we can see that the C# compiler calls: pat.Deconstruct(out var name, out var birthDate);

Here are a few remarks about deconstruction capabilities:

  • The C# compiler expects the method to be named Deconstruct. This is duck typing, this doesn’t work with another method name or if the method Deconstruct() returns something else than void.
  • If you don’t need to deconstruct one or several properties, just use the discard character underscore _ as in : var (name, _) = pat;
  • record and record struct types automatically provides a Deconstruct() method:

  • Several Deconstruct() methods can be provided as long as the number of out parameters is different. If two Deconstruct() methods with the same number of out parameters are provided, at the deconstruction site the compiler emits an error about an ambiguous call:

Ambiguous Deconstruction

  • A method Deconstruct() can be an extension method: this is especially useful to deconstruct a .NET Base Class Library (BCL) class your custom way, like for example to deconstruct a DateOnly :

  • A few types in the .NET BCL provide deconstruction like for example System.Collections.Generic.KeyValuePair<TKey,TValue>. The only other class that offers deconstruction is System.Collections.DictionaryEntry. There are also some Deconstruct() extension methods for Tuple<> as explained in the next section.

Deconstructing Tuples

As anticipated, deconstruction is particularly effective with tuples. This is demonstrated by the following sample program:

Notice that under the hood, the compiler uses the type System.ValueTuple<T1,T2,T3> and keeps track of named parameters city zip country mapping with the corresponding value tuple fields valueTuple.Item1, valueTuple.Item2 and valueTuple.Item3.

ValueTuple<> structures were introduced with C# 7.0 to improve Tuple<> classes in terms of performance and syntax. Deconstruction also works with Tuple<> classes as shown by the program below:

Conclusion

With deconstruction, C# proposes a convenient way to reduce boilerplate code to write more concise code.

Comments:

  1. As written “It allows developers to extract in a single expression, properties of an object or elements of a tuple and then to assign them to distinct variables ”
    In other words one can save lines of code when obtaining several values from an object in several local variables. Thanks to deconstruction this can be done in a single expression.

Leave a Reply

Your email address will not be published. Required fields are marked *