I have previously posted about another exciting and useful C# language enhancements , C# local functions. While creating the code samples for the post I really wanted to include code samples with C# Tuples, but for the sake of brevity and not to over complicate my contrived samples decided to write this separate post.

What are C# Tuples

A tuple is a finite ordered list of elements.

In mathematics, an n-tuple is a sequence (or ordered list) of n elements, where n is a non-negative integer. There is only one 0-tuple, an empty sequence. An n-tuple is defined inductively using the construction of an ordered pair.

Tuples are a popular construct in most functional programming languages such as F#, Haskell, Go, Scala etc. Primarily because it is useful to compose functions directly with other functions that accept tuples. Values are often bundled into one unit conceptually and developers are able pass them all together.

Advantages of tuples

  • Tuples are finite sized in nature i.e. we can’t add/delete elements to/from a tuple.
  • Ability to search any element in a tuple.
  • faster than lists, due to they are constant set of values.
  • Tuples can be used as dictionary keys as they contain immutable values

 

Essential C# 7.0

no-fluff guide to the latest versions of C# for programmers at all levels of experience. Updated to reflect new C# 7.0 features and patterns, it will help you write C# code that’s simple, powerful, robust, secure, and maintainable.
Buy

Why tuples in C# ?

There are many instances during development when you need to pass a lists values to functions for processing.

Typically developers will tackle this problem by either creating KeyValuePair<TKey,TValue> ,  Generic List List Class , Collections or even custom classes.  In most cases, those approaches have and will continue to more than satisfactory or even still the most optimum solution.

Some of you reading this may be thinking,  tuples aren’t anything really new, after all we’ve had the Tuple Class since .Net Framework 4 .

The Tuple Class was not really a C# concept, rather they are .NET types and in reality fall pretty short from being the true tuples. They were limited due in part that the members had fixed names and never really communicated their purpose. They were also reference types which had significant impact on performance besides the fact that they had no syntactical support within C#.

In C#7, tuples have been added to language to provide a concise way to group values together without having to explicitly define specific types.



Using Tuples

In C#7 tuples have been added to the language, and you can use tuple definitions as types to generics making use of the System.ValueTuple , providing first class syntax support.

Tuples are best for internal and private methods rather than public APIs where a static type is much more helpful to the consumer of your API.

Tuple as a return type

You can stipulate a Tuple as return type for a method




Serialization Concerns

Tuples in C#7 are limited serialization capabilities.

Attempting to serialise our tuple to JSON format

Will result in {"Item1":"Gary","Item2":"Woodfine"} being returned. You’ll notice the Keys have been removed.

How the C# 7 ValueTuple type works

In order to understand this behaviour we need to simplify and decompile our code.

You’ll notice that the de-compiled, the GetFreelancer method is simply syntactic sugar and named elements have been pulled out and added via the TupleElementNames attribute. This is because the ValueTuple type’s named elements are erased at runtime, meaning there’s no runtime representation of them.

No Reflection

The absence of named elements in the compiled source means that it’s not possible to use reflection to get those name elements via reflection, which limits ValueTuple’s utility.

This is because under the bonnet the compiler is erasing the named elements and reverting to the Item1 and Item2 properties, meaning our serialiser doesn’t have access to the properties.

Conclusion

Tuples are a great addition to the C# language and can be extremely useful. However, as with any extension or enhancement to software development languages it is vitally important to understand both the advantages and disadvantages in order to make use of them.



Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Latest posts by Gary Woodfine (see all)

π