What is tuple in c#? | New features in C# 5.0 | ASP.NET C# Interview Question | ASP.NET Programmer Guide

, ,
Tuple

Tuples allow you to group related values without the need of a class. It’s more flexible than the KeyValuePair because it can store up to 8 values. A nice thing about Tuples is also the fact that they take on the aggregate equality of the values of the items they hold. Meaning that a Tuple<int, int> with values 1 and 2 equals any other Tuple<int, int> with 1 and 2 as values when you call Equals to compare them, plus they both return the same GetHashCode value.


Here’s a small example of how you can instantiate a Tuple using declarative notation and type inference:

// Create one using the constructor
var tupleOne = new Tuple<int, string, double>(3, "John", 2.13);

// Or using type inference
var tupleTwo = Tuple.Create(3, "John", 2.13);


If you’re thinking of using this for more than 3 items, consider using a class instead because you’re code will end up messy.