Tuple In C# And When To Use It

What is a tuple?

Tuple is a data structure, which consists of an ordered, heterogeneous collection of “n” elements -- the elements in a tuple can either be of the same type or even be of dissimilar types. In Mathematics, an n-tuple may be defined as a sequence or as an ordered list of "n" elements. It should be noted that "n" here denotes a positive integer. Further, there can be only one 0-tuple, i.e., an empty sequence.

The order of the elements in a tuple is defined at the time when the tuple is created. The properties in a tuple are all read-only, i.e., they cannot be changed; once they have been created. The size of the tuple is fixed, since it cannot be changed once; it has been defined at the time when the tuple is created.

Properties of tuple

Immutable- Like strings, tuples in C# are immutable, so once created; no modification is possible and the modification will lead to the creation of another tuple in the memory.

Allocated in Heap- Tuples are classes and not the structures, so the memory of tuples are allocated on the heap and the garbage collector is used to free the memory.

Sequential Memory Allocation- As 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#. 

Advantages and when to use a tuple

  1. Returning multiple values from the function/method.

  2. Composite key in a dictionary.

  3. Easy access and manipulation of the dataset.

  4. Replacement of a class, whose sole purpose is to carry around the data and bind a Combobox or other control.

  5. Pass the multiple values to the function/method.

Disadvantages and when to avoid

  1. Tuples are the classes, not the structs, so it would be allocated on the heap rather than the stack, so it's a kind of overhead on the garbage collector.

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

  3. You can't document the passed in parameters.

  4. Sometimes key-value pairs are not just sufficient for our requirements. Go ahead and try tuples in those scenarios.

We can use a maximum of 8 data types with a tuple.

The overloads are given below.

  1.            Int32 intValue = 1;  
  2.            String stringValue = "Ankush";  
  3.            Boolean boolValue = true;  
  4.            Decimal decimalValue = 1.0M;  
  5.            DateTime datetimeValue = DateTime.Now;  
  6.            float floatValue = 2.22F;  
  7.   
  8.   
  9.            Tuple<Int32> tuple1 = new Tuple<Int32>(intValue);//Create(T1)- Tuple of size 1  
  10.            Tuple<Int32,String> tuple2 = new Tuple<int,string>(intValue,stringValue);//Create(T1,T2)- Tuple of size 2  
  11.            Tuple<Int32,String,Boolean> tuple3 = new Tuple<Int32,String,Boolean>(intValue,stringValue,boolValue); //Create(T1,T2,T3) – Tuple of size 3  
  12.            Tuple<Int32,String,Boolean,Decimal> tuple4 = new Tuple<Int32,String,Boolean,Decimal>(intValue,stringValue,boolValue,decimalValue); //Create(T1,T2,T3,T4) – Tuple of size 4  
  13.            Tuple<Int32,String,Boolean,Decimal,DateTime> tuple5 = new Tuple<Int32,String,Boolean,Decimal,DateTime>(intValue,stringValue,boolValue,decimalValue,datetimeValue);//Create(T1,T2,T3,T4,T5) – Tuple of size 5  
  14.            Tuple<Int32,String,Boolean,Decimal,DateTime,float> tuple6 = new Tuple<Int32,String,Boolean,Decimal,DateTime,float>(intValue,stringValue,boolValue,decimalValue,datetimeValue,floatValue);//Create(T1,T2,T3,T4,T5,T6) – Tuple of size 6  
  15.            Tuple<Int32,String,Boolean,Decimal,DateTime,float> tuple7 = new Tuple<Int32,String,Boolean,Decimal,DateTime,float>(intValue,stringValue,boolValue,decimalValue,datetimeValue,floatValue);//Create(T1,T2,T3,T4,T5,T6,T7) – Tuple of size 7  
  16.            Tuple<Int32, String, Boolean, Decimal, DateTime, float> tuple8 = new Tuple<Int32, String, Boolean, Decimal, DateTime, float>(intValue, stringValue, boolValue, decimalValue, datetimeValue, floatValue);//Create(T1,T2,T3,T4,T5,T6,T7,T8) – Tuple of size 8  

Can we update a tuple?

The answer is no. We can't update a tuple but we can update it by replacing the row item, if we know the index of that particular row. For example;
  1. List<Tuple<String>> objTuple = new List<Tuple<String>>();  
  2. objTuple.Add(new Tuple<string>("Ankush"));  
  3. objTuple.Add(new Tuple<string>("Prashant"));  
  4. objTuple.Add(new Tuple<string>("Sachin"));  
  5. objTuple[1] = Tuple.Create("Anku");  
  6. var res = objTuple;  
Ebook Download
View all
Learn
View all