NDepend

Improve your .NET code quality with NDepend

Deconstruction in C#

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.

My dad being an early programmer in the 70's, I have been fortunate to switch from playing with Lego, to program my own micro-games, when I was still a kid. Since then I never stop programming.

I graduated in Mathematics and Software engineering. After a decade of C++ programming and consultancy, I got interested in the brand new .NET platform in 2002. I had the chance to write the best-seller book (in French) on .NET and C#, published by O'Reilly and also did manage some academic and professional courses on the platform and C#.

Over my consulting years I built an expertise about the architecture, the evolution and the maintenance challenges of large & complex real-world applications. It seemed like the spaghetti & entangled monolithic legacy concerned every sufficiently large team. As a consequence, I got interested in static code analysis and started the project NDepend in 2004.

Nowadays NDepend is a full-fledged Independent Software Vendor (ISV). With more than 12.000 client companies, including many of the Fortune 500 ones, NDepend offers deeper insight and full control on their application to a wide range of professional users around the world.

I live with my wife and our twin kids Léna and Paul in the beautiful island of Mauritius in the Indian Ocean.

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 *