Working With Tuples In C#

Introduction

In our day-to-day work we use Classes and Struct, we create objects of classes and use them. Sometimes we only need few properties from a class and we do not want to create complete object of it, in this scenario Tuples in C# come to the rescue.

What are Tuples?

Tuples are a type of data structure that can carry multiple data elements of different data types. It is a light weight data structure. In simple words, Tuples is a variable that can contain multiple variables of different data types.

(double, int) tuple1 = (1, 2);
Console.WriteLine($"Tuple elements { tuple1.Item1} and { tuple1.Item2}.");

In the above line of code you can see that tuple1 contains a double and int. Let’s update the above code.

(double, int, string) tuple2 = (1, 2, "Talha");
Console.WriteLine($"Tuple elements {tuple1.Item1} , {tuple1.Item2} and {tuple2.Item3}");

As you can see we have added a string in tuple. Now this shows that there is no limit in tuple data structure we can add as many items as we want.

You can also explicitly specify the tuple fields both during initialization or in the definition.

(double EmpId, int EmpAge, string EmpName) tuple3 = (1, 35, "Talha");
Console.WriteLine($"Tuple elements {tuple3.EmpId} , {tuple3.EmpAge} and {tuple3.EmpName}");
var tuple4 = (EmpId: 1, EmpAge: 35, EmpName: "Talha");
Console.WriteLine($"Tuple elements {tuple4.EmpId} , {tuple4.EmpAge} and {tuple4.EmpName}");

Tuples in functions

One of the best use of tuples that I have found is in return of functions. If you have a class that contains up to 20 properties and you just need two or three of them then despite returning complete object just use tuples.

Console.WriteLine($"Employee Profile: FirstName { Profile() }");
public static (string, string, int) Profile()
{
  (string, string, int) profileTuple = ("Steve", "Jobs", 50);
  return profileTuple;
}

This is the real scenario that I face once and thanks for the Tuples it saved my day.

Similarly, we can also pass Tuple as a parameter in function.

public static void Profile((string, string, int) empData)
{
 Console.WriteLine($"Employee Profile: FirstName { empData.Item1 }, LastName {empData.Item2}, Age {empData.Item3}");
}

You can also convert Tuple to JSON.

Console.WriteLine($"Serilized Tuple { JsonConvert.SerializeObject(("Steve", "Jobs", 50))}");

Assignment

As in this tutorial above I mentioned that is simple words Tuples are variables. This means that we can do all the operations of a variable on Tuple like assignment. We can assign value of one tuple to another.

var tuple5 = (1, 2, 3);
var tuple6 = (4, 5, 6);
Console.WriteLine($"Tuple5 {tuple5}");
Console.WriteLine($"Tuple6 before assignment {tuple6}");
tuple6 = tuple5;
Console.WriteLine($"Tuple6 after assignment {tuple6}")

To assign tuple you have to take care of the following points

  • Both tuple must have same number of elements
  • And their data type must be same from right to left

Forms of Tuples

There are two different forms of Tuples System.Tuple and System.ValueTuple

ValueTuples are value types means they contain the instance of the type its data members are fields and are immutable but Tuples are reference types and are mutable and its data members or elements are properties.

Summary

In this article, we discussed about Tuples how we can use this data structure to store multiple elements of different data types and how we can use it in functions both as a parameter and as a return type.