Posts Tagged ‘Tuples’

Deconstruction in C#7.0

Posted: July 29, 2017 in .NET, C#
Tags: , ,

In my earlier blog post related to Tuples, I had used the following two ways for accessing the elements of the tuple.

image

image

The first one is obviously inefficient and to be honest wrong, there is no point calling the method twice. The second one can be simplified further as shown in the snippet below:

image 

This is referred to as the deconstruction of the tuple.

This technique of deconstruction can be now applied to any . NET class/struct , by implementing the Deconstruct method.

image

The object instance can be then deconstructed back into three variables as shown below:

image

The code shown above is nothing but the syntactic sugar coat. The decompiled code clearly shows the Deconstruct method of the Person class getting invoked and the variables a , b & c does not exists in the complied code. These are replaced by variables declared with names same as the parameters of the Deconstruct method implementation.

image

Tuples in C#7

Posted: July 29, 2017 in .NET, C#
Tags: , ,

What is a tuple ? -  A tuple is an ordered group of elements.

Tuple has been there as a data type in most of the functional programming languages like Lisp , Haskell since their inception.

For example in Haskell , tuple provides a mechanism to store multiple values together. One key difference with a List is, tuple allows to store multiple values of different data type.

image

In C#4.0, Generic Tuple class(es) was introduced, the primary intent was to make it interoperable with languages like Python / F#. This also provided an easy way to return multiple values from a method without defining a separate class or structure.

image

The tuple values could be accessed using the Item property of the tuple class as shown below:

image

We will tweak the above code as follows:

image

We will not be able to modify the element values as these Item properties are read-only making the tuples immutable.

image

There are about 8 overloads of Tuple.Create method and 8 Tuple classes allowing us to tuples up to 8 elements.

So far so good , but if I compare the C# snippet with it is Haskell counterpart it looks cumbersome. It looks kludgy , as if some feature has been artificially dumped into the language.

This is further improved in C#7 with elegant syntactic sugar coat and the System.ValueTuple structure doing the work in background.

This feature is now built into the framework (4.7) and if you are using 4.6.2 or earlier,  the package reference to System.ValueTuple needs to be added.

image 

Like me , if you are using Visual Studio Code you just need to run the command β€œdotnet add package "System.ValueTuple" from the Terminal, followed by a β€œdotnet restore”.

image

We can now refactor our earlier code using much simpler and more elegant syntax as shown below:

image

The tuple values could be accessed similarly using the Item property as shown below:

image

We can further improve this by leveraging the new feature of providing tuple element names.

image

These element names are used while accessing the values, making the code more readable.

image

Unlike Tuple classes , the ValueTuple structure is mutable and it allows us to modify the values.

image

The decompiled ( by dotPeek) shows how ValueTuple structure is doing it’s job. The code below looks quite similar to what we did earlier with Tuple class. The named tuple elements are implemented using the attribute System.Runtime.CompilerServices.TupleElementNames. TupleElementNames stores the element names as a string array.

image

image