Tuples in C#

In this article you will learn about tuples in the C# language.

As a programmer we love to write more with less lines of code and a tuple is one of those features of C# that when used properly can help the programmer drastically reduce the lines of code.

So what on Earth is a tuple? A tuple is basically a  data-structure. A tuple has a specific number and order/sequence of elements. The .NET Framework directly supports tuples with one to seven elements. In addition, you can create tuples of eight or more elements by nesting tuple objects in the Rest property of a Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> object. 
Properties of tuple
  • Immutable: Like strings, tuples in C# are immutable, so once created no modification is possible, modification will lead to creation of another tuple in the memory.
  • Allocated in Heap: Tuples are classes not structures, so the memory of tuples are allocated on the heap and the garbage collector is used to free the memory.
  • Sequential Memory Allocation: like arrays, tuples are allocated in sequential memory.
  • Fixed Size: A tuple, once created, can't be modified in terms of size, so a singleton will be only used as a singleton, the same applies to other categories of tuples.
  • Heterogeneous Nature: Like arrays of objects, tuples can be used to store various types, they can store user defined types and default types of C#.
Categories Of Tupples

Name Code Description
Singleton Create<T1>(T1) Create a new 1 Tuple
Pair Create<T1,T2>(T1,T2) Create a new 2 Tuple
Triple Create<T1,T2,T3>(T1,T2,T3) Create a new 3 Tuple
Quadruple Create<T1,T2,T3,T4>(T1,T2,T3,T4) Create a new 4 Tuple
Quintuple Create<T1,T2,T3,T4,T5>(T1,T2,T3,T4,T5) Create a new 5 Tuple
Sextuple Create<T1,T2,T3,T4,T5,T6>(T1,T2,T3,T4,T5,T6) Create a new 6 Tuple
SepTuple Create<T1,T2,T3,T4,T5,T6,T7>(T1,T2,T3,T4,T5,T6,T7) Create a new 7 Tuple
Octuple Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1,T2,T3,T4,T5,T6,T7,T8) Create a new 8 Tuple

So as we can, we can create 8 types of tuples in C# 4.0 (and later versions) of the preceding tuples. Octuples are the most interesting tuple, in an Octuple the T8 parameter itself is a tuple, so its like if you have more than 7 parameters then you should use an Octuple to pass more than 7 parameters.

Let's say we have a simple class, say a login class where we want to store the username, password and email of the user on the form, then instead of creating a class for that, we can have a tuple like:
  1. class DemoClass  
  2. {  
  3.     Tuple<stringstringstring> userLoginTupple;  
  5.     void setuserTupple ()  
  6.     {  
  7.         userLoginTupple = new Tuple<string,string,string>("pankajBhandari","pankajbhandari@gmail.com","myPassword");  
  8.     }  
  9. }  
And then to get the values from the tuple we  can use the tuple as:
  1. partial class DemoClass  
  2. {  
  3.     void GetVallueFromTheTupple(Tuple<stringstringstring> userLoginTupple)  
  4.     {  
  5.         var userName = userLoginTupple.Item1;  
  6.         var email = userLoginTupple.Item2;  
  7.         var password = userLoginTupple.Item3;  
  8.     }  
  9. }  
Tuples can store the values of multiple datatypes, they can hold user-defined types and basic types of .NET, so the following  examples are perfectly  valid  examples of tuples:
  1. var userLoginTupple = new Tuple<string,string,string>("pankajBhandari","pankajbhandari@gmail.com","myPassword");  
  2. var stringAndIntTupple = new Tuple<stringint>("pankaj", 2);  
  3. var floatAndIntTupple = new Tuple<intfloat>(12, 12f);  
  4. var userDefinedTypeTuple = new Tuple<myClass1, myClass2>(new myClass1(), new myClass2());  
  5.       var thisIsALongTupple = new Tuple<intstringfloatdoubleboollongshort, Tuple<stringstringstring>>(12, "pankaj", 12f, 12.00, false, 12, 12, userLoginTupple);  
So in the preceding example we have various tuples and most of them are self-explanatory, of the preceding I would like to say something about the last tuple, thisIsALongTupple. The point to note regarding the preceding tuple is "A tuple can have only 7 parameters at most, if you need to pass more than 7 parameters to the tuple you need to pass the 8th parameter as a tuple".
So that was all about tupples and since every coin has two sides let's see the advantages and disadvantages of tuples.
Advantages and when to use a tuple  
  •  Returning multiple values from the function/method.

  •  Composite Key in a dictionary.

  •  Easy access and manipulation of dataset.

  •  Replacement of a class whose sole purpose is to carry around data and bind a combobox or other control.

  •  Pass multiple values to the function/method.
Disadvantages and When to avoid. 
  •  Tuples are classes, not structs, so it would be allocated on the heap rather than the stack so its kind of overhead on the garbage collector.

  •  Tuples are not so syntactically mature so the code is less readable, so use it for internal classes but avoid exposing them to external classes.

  • You can't document the passed in parameters.

  •  Sometimes key-value pairs are not just sufficient for our requirements, go ahead and try tuples in those scenarios.
Personally I love to use tuples when I must pass 2-3 values to a function or return multiple values from the function. Most commonly for returning multiple values from a function.
So that was all about tuples, I hope you guys like it. :)