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
- using System;
-
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- Tuple<int, string, string> tupleEmp = new Tuple<int, string, string>(1, "Kirtesh", "shah");
-
-
- Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);
- Console.ReadLine();
- }
- }
- }
Using Create Method
- using System;
-
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- var tupleEmp = Tuple.Create(1, "Kirtesh", "shah");
-
-
- Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);
- Console.ReadLine();
- }
- }
- }
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.
- using System;
-
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- var tupleEmp = Tuple.Create(1, "Kirtesh", "shah");
-
-
- Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);
- Console.ReadLine();
- }
- }
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,
- using System;
-
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- var tupleEmp = (1, "Kirtesh", "shah");
-
-
- Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Item1, tupleEmp.Item2 + " " + tupleEmp.Item3);
- Console.ReadLine();
- }
- }
- }
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,
- using System;
-
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- var tupleEmp = (Id: 1, FirstName:"Kirtesh", LastName:"shah");
-
-
- Console.WriteLine("Emp ID {0} : And Emp Name : {1}", tupleEmp.Id, tupleEmp.FirstName + " " + tupleEmp.LastName);
- Console.ReadLine();
- }
- }
- }
In the above code , we have created "Named Tupe" and also access tuple with name.
Let’s try to use Named tuple in method,
- using System;
-
- namespace TupleDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var empTuple = GetEmployee(100);
-
-
- Console.WriteLine("Emp ID {0} : And Emp Name : {1}", empTuple.Id, empTuple.firstName + " " + empTuple.lastName);
- Console.ReadLine();
- }
-
- public static (int Id,string firstName,string lastName) GetEmployee(int empId)
- {
-
- string firstName = string.Empty;
- string lastName = string.Empty;
-
-
-
- if(empId==100)
- {
- firstName = "Kirtesh";
- lastName = "Shah";
- }
- return (empId, firstName, lastName);
- }
- }
- }
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.