C# - Introduction Of Tuple

In this article, we are going to discuss the wonderful feature “Tuple” introduced in C# 6.0 and improved in C# 7.0.
 
The first question which comes to our mind Why it is required?
 
Prior to C# 6.0, how will you return multiple outputs from a method? The Common answer would be out or ref parameters.
 
But what if your method has 7-8 outputs? your code can become complicated, Right? So, To simplify your coding, Microsoft has introduced a tuple in C# 6.0. Using tuple you can group method outputs and access them easily.
 

What is a tuple?

 
A tuple is a lightweight data structure which contains a sequence of same or different data type.
 
Below are few important points of the tuple,
  • It introduced in .Net Framework in 4.0
  • It is inherited from the Object class.
  • Name Space: System
  • Class: Tuple 
  • Tuple can take up to 8 values.
  • Can be nested for more than 8 values.
  • It is a reference type, not a value type.

How to create a tuple?

 
Normally below two methods used to create a tuple,
 
Using Constructor 
  1. using System;  
  2.   
  3. namespace TupleDemo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             //Using Constructor   
  10.             Tuple<intstringstring> tupleEmp = new Tuple<intstringstring>(1, "Kirtesh""shah");  
  11.   
  12.             // Get values from Tuple  
  13.             Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17. }  
Using Create Method
  1. using System;  
  2.   
  3. namespace TupleDemo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             //Using Create Method   
  10.             var tupleEmp = Tuple.Create(1, "Kirtesh""shah");  
  11.   
  12.             // Get values from Tuple  
  13.             Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17. }  

How to Access and return Tuple Values?

 
A disadvantage of Tuple in c# 6.0 is, it’s not strongly typed and needs to access the value using the below method. In C# 7.0 Microsoft tries to give a solution to some extend.
  1. using System;  
  2.   
  3. namespace TupleDemo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             //Using Create Method   
  10.             var tupleEmp = Tuple.Create(1, "Kirtesh""shah");  
  11.   
  12.             // Get values from Tuple  
  13.             Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
Tuple can contain output values as Item1, Item2,.. etc as per the above code snap. So you need to remember the meaning of Item1, Item2 …etc in C# 6.0. Microsoft has given a workaround in C# 7.0 which we will discuss below in this article.
 
The output of the above code would be,
 
 

Tuple - Improvement in C# 7.0

 
A tuple used in C# 6.0 is not a strongly type tuple and has a performance issue, hence Microsoft introduces “ValueTuple” in C# 7.0. It leverages the new Value type struct instead of the Tuple class in C# 6.0.
 
To add “Value Tuple” in Visual studio 2017 or prior, we need to add Nuget Package,
 
 
Lets create a value Tuple, 
  1. using System;  
  2.   
  3. namespace TupleDemo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             //Create Value Tuple    
  10.             var tupleEmp = (1, "Kirtesh""shah");  
  11.   
  12.             // Get values from Tuple  
  13.             Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17. }  
In the above code, Value Tuple – tupleEmp was created without Create method or Tuple class constructor. The Tuple still contains output values as Item1, Item2,.. etc as per the above code snap.
 
To overcome output value issues, Microsoft introduces “Named Tuple” in C# 7.1.
 
Named Tuple
 
A named tuple is the same as Value Tuple, but has an option to name each item. See below code,
  1. using System;  
  2.   
  3. namespace TupleDemo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             //Create Value Tuple    
  10.             var tupleEmp = (Id:  1, FirstName:"Kirtesh", LastName:"shah");  
  11.   
  12.             // Get values from Tuple  
  13.             Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Id, tupleEmp.FirstName + " " + tupleEmp.LastName);  
  14.             Console.ReadLine();  
  15.         }  
  16.     }  
  17. }  
 
In the above code , we have created "Named Tupe" and also access tuple with name.
 
Let’s try to use Named tuple in method,  
  1. using System;  
  2.   
  3. namespace TupleDemo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             var empTuple = GetEmployee(100);  
  10.   
  11.             //// Get values from Tuple  
  12.             Console.WriteLine("Emp ID {0} : And Emp Name : {1}", empTuple.Id, empTuple.firstName + " " + empTuple.lastName);  
  13.             Console.ReadLine();  
  14.         }  
  15.   
  16.         public static (int Id,string firstName,string lastName) GetEmployee(int empId)  
  17.         {  
  18.             //  
  19.             string firstName = string.Empty;  
  20.             string lastName = string.Empty;  
  21.               
  22.             // It should be from DB but here i have hardcoded  
  23.   
  24.             if(empId==100)  
  25.             {  
  26.                 firstName = "Kirtesh";  
  27.                 lastName = "Shah";     
  28.             }  
  29.             return (empId, firstName, lastName);  
  30.         }  
  31.     }  
  32. }  
In the above code, we have created the method GetEmployee which is taking EmpId as an input parameter. It will fetch emp details from DB (Here Hardcoded) and return all employee details using Tuple. As an employee has different attributes like id, firstname, lastname, and returns all these attributes values from a method.
 
In the below case we have three attributes Id ,firstName, lastName in named tuple.
 
 
The below code will show how to access and get value from the tuple.
 
 
That’s all for this article. I have tried to explain Tuple in detail and hope you enjoy this article.