In this article, we will learn about Tuple in C#.
What is a Tuple?
A tuple is a reference type lightweight data structure that may contain different data types. It is introduced in C# 7.0. Tuple is useful when we want to store data of different data types without creating a new one.
Syntax to create a Tuple
var tuple1 = new Tuple<string, int, string, string, int>
The following code snippet is used to create a tuple with 4 elements of different data types.
Tuple<int, string, string, string> employee = new Tuple<int, string, string, string>(1, "Ram", "Kumar", "India")
C# Tuple using Create() Method
We can also use the Create()
method to create a tuple. So in this way, we can create a tuple without mentioning the data types of the tuple elements.
Syntax to create a tuple with Create() method:
var tuple1 = Tuple.Create(item1, item2, item3,............item8);
So, let’s understand this with the help of a basic code snippet:
var product = Tuple.Create("Laptop", "Dell", 4000, "USD", 2);
Here, we use Tuple.Create()
method to create a tuple and we are not defining the data type of any element here.
Now, we will access the tuple items:
Console.WriteLine(product);
Console.WriteLine($"Product: {product.Item1}");
Console.WriteLine($"Brand: {product.Item2}");
Console.WriteLine($"Cost: {product.Item3}");
Let’s see the output in the console:
(Laptop, Dell, 4000, USD, 2)
Product: Laptop
Brand: Dell
Cost: 4000
Create()
method of Tuple can include a maximum of 8 elements. A tuple with 8 elements is referred to as an octuple. It will throw a compile-time error if we will add the 9th element.
So, the below code snippet below will throw the error :
var data = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8,9);
How can we access Tuple elements?
Use Item<Element Index Number>
e.g. Item1, Item2, and so on to access Tuple elements.
Refer to the code snippet:
var data = Tuple.Create(1,"Ram","Kumar","India");
Console.WriteLine("ID - " + data.Item1);
Console.WriteLine("First Name - " + data.Item2);
Console.WriteLine("Last Name - " + data.Item3);
Console.WriteLine("Country - " + data.Item4);
Nested Tuple
As we have learned a Tuple can not include more than 8 elements. A nested Tuple will help to add more than 8 elements to it.
Syntax to use nested Tuple –
var data = Tuple.Create("Delhi","London","Washington","Toronto","Sydney","Auckland","Bangalore", Tuple.Create("Chennai","Tokyo","Mumbai"));
How to access Nested Tuple elements?
To access normal Tuple elements we can use Item1, Item2 and so on. But to access nested Tuple we will use the Rest keyword.
The below code snippet will display the – Delhi
Console.WriteLine(data.Item1);
To access nested Tuple elements we will use the below code. This code snippet will display Chennai. You may notice that the below code contains Item1 2 times, this is because the Tuple elements are nested.
Console.WriteLine(data.Rest.Item1.Item1);
Now, what will happen if we will use data.Rest.Item1
This will give the below output –
Q. Can we use Tuple as a method parameter?
Answer – Yes, we can use Tuple as a method parameter.
Q. Can we use Tuple as a return type?
Answer – Of course, we can use Tuple as a method parameter.
Why to use Tupple in C#?
There are numerous reasons to use Tuple in our application. Some of the reasons are –
Return Multiple Data Type Values Using Tuple in C#
public static Tuple<bool, int, int[], string> ReturnMultipleValueWithTuple(int num)
{
}
Here, the method ReturnMultipleValueWithTuple()
accepts a parameter and then returns a Tuple which has bool
, int
, integer
array
and string
. So, in this way, we can use Tuple to return multi-data types.
Pass Multiple Data Type Values to a Method Using Tuple in C#
public static string MethodWithTupleParameter(Tuple<string, int, bool> tuple)
{
}
Here, the method MethodWithTupleParameter()
accepts a Tupple as a parameter. The tuple in the parameter has string
, int
and bool
data type.
Takeaway-
A tuple is a data structure which is introduced in C# 7.0. It can include 8 elements, we can use nested Tuple if we want to add more elements. A tuple can be used as a method parameter and return type.
We learnt the benefits of using Tuple in our code.
Hope this article is helpful to you.