‘Stupid’ Question 14: What is a ’Tuple’ in C#, and what can we use it for?
What is a ’Tuple’ in C#, and what can we use it for?
So, I’ve heard about these ‘Tuple’ - but never used it or spent too much time thinking about them. I actually thought it was in the collection family, some sort of dictionary or list. But I decided today to take a few minutes and give it a good Google, and with that I present to you the answer:
Tuple is a generic static class that was added to C# 4 and it can hold any amount of elements, and they can be any type you want. To hold more than 8 elements you will need to nest tuple objects.
After spending some time on SO and MSDN and various sites I found the following use-cases:
- Passing multiple values to a method that only accepts a single parameter.
- Return multiple values from a method – but don’t want to define a class (this example was the most popular one)
- Represent a single set of data (a lot of debate around this one)
Generally I got the impression that Tuple gets overused for convenience sake, and that tuples are more useful in functional languages such as F# ( which seems to be the driving force behind the creation of the Tuple in C#).
Hmm… honestly ,- my first impression was ‘Nice! How convenient!’ – But then I tried to use it and the code just stared back at me, it looked ugly. You know what I mean, when you just get the gut feeling that it doesn’t look/seem right. It seemed bulky and bloated. So I removed it. It might come in handy one day, but today wasn’t the day.
Last modified on 2012-08-03