C#.NET - Tuple

Today we are going back to basics and walk through what are Tuples in C#.NET and how to use them.

The feature Tuple is not something new and it was introduced in .NET Framework 4.0. Tuple is a data structure which holds the data of different data types in sequential order as shown below:

[pre class="brush:csharp; toolbar: false;" title=""] Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> [/pre]

Why Tuples?

Prior to .NET Framework 4.0 if you want to return multiple values from a method you might using one of below:
  • Creating a temporary class and return it.
  • Have multiple ref/out parameters to a method.
  • Create a dynamic object and return it.

Instead, now you can just use the Tuple object and return it.



Create Tuple

The Tuple<T> class exists in System namespace. Tuple has a capacity of storing maximum of eight elements in it. You will get a compilation error when you try to add more than eight elements.

There are two ways to create the Tuple One is using the Constructor of the Tuple class and another is using a static method Create on the Tuple class.

The below code snippet shows how to create a Tuple using Constructor approach.

[pre class="brush:csharp; toolbar: false;" title="create Tuple using constructor"]
//create Tuple using constructor Tuple<int, string, string, string, long, DateTime> employee1 = new Tuple<int, string, string, string, long, DateTime> (1, "Srinivasa Rao", "Dhulipalla", "email@domain.com", 9876543210, DateTime.Now);
[/pre]

The drawback of creating the Tuple using constructor method is you have to specify the data type of each element and it is cumbersome. C# offers a static method called ‘Create’ on Tuple class without specifying the data type of each element.

The below code snippet will tell how to create the Tuple using a Create method.

[pre class="brush:csharp; toolbar: false;" title="create Tuple using Create method"] //create Tuple using Create method var employee2 = Tuple.Create(2, "Srinivasa Rao", "Dhulipalla", "email@domain.com", 9876543210, DateTime.Now); [/pre]

Access Tuple data

Since the Tuple stores the elements in sequential order in it, the elements data also accessed in the same order using Item<elementOrderNumber> on Tuple object. 

Ex: Tuple elements can be accessed using Item1, Item2, Item3, Item4, Item5, Item6 and Item7 properties of Tuple object. The last (eight) element will be accessible using Rest property.

[pre class="brush:csharp; toolbar: false;" title="create Tuple using Create method"]
//create Tuple using Create method
var colors = Tuple.Create("Red", "Green", "Blue", "Yellow", "Black", "White", "Orange", "Pink"); colors.Item1; //returns Red
colors.Item2; //returns Green
colors.Item3; //returns Blue
colors.Item4; //returns Yellow
colors.Item5; //returns Black
colors.Item6; //returns White
colors.Item7; //returns Orange
colors.Rest; //returns Pink
[/pre]

In general, the eight element is for nested Tuples and can be accessible using Rest property. But it is not mandatory to represent the nested Tuples as a last element and can be anywhere in the Tuple sequence. 

Nested Tuples

As we know by now, Tuple can be used to specify a maximum of eight elements. But if you want to add more than eight elements you can do it by nesting the other Tuple either in last position or somewhere in the sequence of elements.

[pre class="brush:csharp; toolbar: false;" title="create Tuple using Create method"]
//create Tuple using Create method
var months = Tuple.Create("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", Tuple.Create("Aug", "Sep", "Oct", "Nov", "Dec"));

months.Item1; //returns Jan
months.Item2; //returns Feb
months.Item3; //returns Mar
months.Item4; //returns Apr
months.Item5; //returns May
months.Item6; //returns Jun
months.Item7; //returns Jul
months.Rest.Item1; //returns (Aug, Sep, Oct, Nov, Dec)
months.Rest.Item1.Item1; //returns Aug
months.Rest.Item1.Item2; //returns Sep
months.Rest.Item1.Item3; //returns Oct
months.Rest.Item1.Item4; //returns Nov
months.Rest.Item1.Item5; //returns Dec
[/pre]

Tuple as Method Parameter

You can pass the Tuple object as a parameter to a method which is required multiple input parameters.

[pre class="brush:csharp; toolbar: false;" title="pass Tuple as a parameter to a method"]
//pass Tuple as a parameter to a method
PrintEmployee(employee1);

private void PrintEmployee(Tuple<int, string, string, string, long, DateTime> employee)
{
   Console.WriteLine(employee.Item1);
   Console.WriteLine(employee.Item2);
   Console.WriteLine(employee.Item3);
   Console.WriteLine(employee.Item4);
   Console.WriteLine(employee.Item5);
   Console.WriteLine(employee.Item6);
}
[/pre]

Tuple as Method Return Type

When you need to return multiple parameters from a method, you can just create the Tuple object and return it as shown below:

[pre class="brush:csharp; toolbar: false;" title="call a method which returns the Tuple object"]
//call a method which returns the Tuple object
var employee = GetEmployee();

private Tuple<int, string, long, DateTime> GetEmployee()
{
    return Tuple.Create(1, "Srinivasa Rao Dhulipalla", 9876543210, DateTime.Now);
}
[/pre]

Tuple Limitations

Though there are various advantages of using Tuples as we seen above, at the same there are couple of limitations on it.
  • Tuple has limit of only storing eight elements and the workaround is usage of nested Tuples to extend with more elements. But this approach doesn't make sense when you have multiple levels of Tuples and makes you complicate to maintain the code.
  • Accessing the Tuple element by its sequential order. You have to always remember the order and type of element to be stored.

That’s it. We have learned the Tuples in detail with examples on how to use Tuple in C#.NET.
Any questions further? Shoot them under the comments section below.

No comments:

Powered by Blogger.