Join Reynald Adolphe for an in-depth discussion in this video Tuples overview, part of C# 7 First Look.
- [Instructor] When discussing new features of C# 7 you might hear that they've added tuples, but you may also hear that tuples have already existed, which they did but there is a difference. So before we get into that let's go ahead and have a refresher on what a tuple exactly is. It's essentially a data structure that provides an easy way to represent a single set of data. And it can hold any number of elements and they can be of any type that you want. A tuple is also generic class that was added in an earlier version of C#, however with C# 7, it's a value type.
however with C# 7, it's a value type. And the comparison breaks down like this: the old tuple was a reference type, and it was immutable, it had to be, otherwise when dealing with state there could be a great possibility of having issues. The new tuple is a value type, because it's a struct as opposed to a class. And this time it's mutable. C# 7 extends the tuples functionality by adding tuple types and tuple literals.
Let's go ahead and take a look at some examples starting off with how tuples were originally implemented in .net.
- Literals, separators, and local functions
- Is expressions with patterns
- Switch statements with patterns
- Ref returns
- Out variables
- Async return types and expressions
- Throw expressions