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)
- {
- //Using Constructor
- Tuple<int, string, string> tupleEmp = new Tuple<int, string, string>(1, "Kirtesh", "shah");
- // Get values from Tuple
- 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)
- {
- //Using Create Method
- var tupleEmp = Tuple.Create(1, "Kirtesh", "shah");
- // Get values from Tuple
- 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)
- {
- //Using Create Method
- var tupleEmp = Tuple.Create(1, "Kirtesh", "shah");
- // Get values from Tuple
- 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,





Join the conversation! Your thoughts help the community grow.