NDepend Blog

Improve your .NET code quality with NDepend

Deconstruction in C#

January 18, 2023 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 rewritten sample we can see that several lines of code can be saved when obtaining from an object several values within 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 deconstruction site the compiler emits an error about ambiguous call:

Ambiguous Deconstruction

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

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

Deconstructing Tuples

As one could expect, deconstruction works especially well with tuples. This is shown by this sample program:

Notice that under the hood, the compiler use 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<> 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 in order to write more concise code.

Comments:

  1. Dennis Jensen says:

    Why?

  2. 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.

Comments are closed.