DEV Community

Milecia
Milecia

Posted on • Updated on

What is a tuple in C#?

This isn't something that most web developers know about because tuples aren't commonly used in web development. It's not because they aren't useful because they definitely can be. A tuple is just one of the many good things that gets buried under all of the other good things. So let's dig it up and polish it.

To start with, a tuple is a data structure that has a number of elements with different data types. An important thing to keep in mind is that a tuple is a reference type instead of a value type. That means it doesn't store the data in its own memory space. These store the location of where the data is in memory. Classes are a good example of how a reference type works with the memory.

Anyways, tuples are used in a few ways.

  • When you want to hold some values temporarily without making a separate class. It can also be used to hold a database record.

  • You can return multiple values from a method without needing the out or ref parameters.

  • You can also pass multiple values to a method with just one parameter.

Now you've dug up what a tuple is, it's time to start polishing it. One thing that makes tuples distinct is that they are immutable. That means you can't change them at all once they have been created. Any properties in that tuple are going to be read-only. It also means that the size of the tuple is fixed after you create it.

There is some good news! In C# 7, tuples have been updated and made better. You don't have to worry about how many values your tuple has (originally, you could only have eight values in a tuple). You can also name your tuple properties instead of using the default super generic property names. And the best part is that all you have to do is install the System.ValueTuple NuGet package.

Hopefully this shed some light on one of the more obscure development tools available to you. Do you have any experience with tuples? Even I don't know a lot of people who do so I'd love to hear what you think in the comments.


Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding

Top comments (5)

Collapse
 
goaty92 profile image
goaty92

Well, there is a difference between the System.Tuple types and the new ValueTuple that has been added since C# 7. In fact ValueTuple is a value type (not a reference type) which brings additional performance benefit and should be prefered over System.Tuple.

Collapse
 
joaofbantunes profile image
João Antunes

Since you're mentioning that a Tuple is a reference type and then mention the recently added ValueTuple, you could further complete the information with the fact that the ValueTuple is a struct (value type) 👍.

Collapse
 
mteheran profile image
Miguel Teheran

Important take into account that we need to use framework 4.6.2 in order to implement tuples and it has a feature for 7.1.

7.0:
int count = 5;
string label = "Colors used in the map";
var pair = (count: count, label: label);

7.1 (new feature default names):
int count = 5;
string label = "Colors used in the map";
var pair = (count, label); // element names are "count" and "label"

Collapse
 
dance2die profile image
Sung M. Kim

I love how C# is embracing good parts from other languages.

I use the 7.1 syntax in Javascript quite much and been tripping me much in C#.

Collapse
 
tux0r profile image
tux0r

a tuple is a data structure that has a number of elements with different data types

Mathematically, a tuple is a "sorted list".