A tuple, introduced in .Net version 4.1, can be defined as a data structure having a specific number and sequence of elements.For example, a tuple with two elements:
In the above line of code, a tuple is created with two different data type items. The tuple is supposed to store the ID and Name.
The tuple supports up to seven elements directly. That is:
If you want eight or more then you use the nested tuple objects in the Rest property of the tuple (see the last example).
The following section details how to create a tuple, access its items, and other aspects with examples.
An example of creating a tuple in C#
This example shows how to create a C# tuple with four items.
I used int, string, decimal, and string types for the items for storing the ID, name, salary, and joining date of the employee.
Have a look at creating, assigning values, and then accessing the tuple items:
using System; class tuple_example { static void Main() { Tuple<int, string, decimal, string> emp_tuple = new Tuple<int, string, decimal, string>(001, "Mike", 4500.00, "20 June 2015"); Console.WriteLine("ID: " + emp_tuple.Item1); Console.WriteLine("Name: " + emp_tuple.Item2); Console.WriteLine("Salary: " + emp_tuple.Item3); Console.WriteLine("Join Date: " + emp_tuple.Item4); Console.ReadLine(); } }
The result:
You can see the tuple items are accessed by Item1, Item2 and so on properties. You may use up to seven elements while the eighth element is the Rest property.
Creating a tuple without specifying the type
A tuple can also be created without specifying the type of items. For that, you may use the helper class Tuple.
The example below shows how to create the same tuple as in the above example without the item types:
using System; class tuple_example { static void Main() { var emp_tuple_noType = Tuple.Create(008, "Anna", 3300.00, "13 Aug 2018"); Console.WriteLine("ID: " + emp_tuple_noType.Item1); Console.WriteLine("Name: " + emp_tuple_noType.Item2); Console.WriteLine("Salary: " + emp_tuple_noType.Item3); Console.WriteLine("Join Date: " + emp_tuple_noType.Item4); Console.ReadLine(); } }
The output should be the same as in the above example except for the difference in item values.
Using tuple in Dictionary example
You may also use a tuple with Collections like List, Dictionary, etc. In the following example, we have created a Dictionary object and used a tuple for creating a telephone directory (for the sake of demo only).
using System; using System.Collections.Generic; class tuple_example { static void Main() { var tel_dir = new Dictionary<Tuple<int, string, string>, string>(); tel_dir.Add(Tuple.Create(1, "Hina", "house no - address here"), "454 457 4547"); tel_dir.Add(Tuple.Create(2, "Mike", "address here"), "001 121 4144"); tel_dir.Add(Tuple.Create(3, "Tamanna", " address here"), "001 242 1454"); tel_dir.Add(Tuple.Create(4, "Tahir", "address here"), "001 121 1414"); foreach (var dir in tel_dir) { System.Console.WriteLine(dir); } Console.ReadLine(); } }
The output:
The directory shows the serial no, name, address, and telephone number as the output as we iterate through the tuple.
In real applications, the source of tuple values can be database-driven and each record can be added by using the add method of Dictionary or List<T>.
Creating a nested tuple example
The following example shows how to create a nested tuple.
using System; class tuple_example { static void Main() { var neted_tup = Tuple.Create(1, "Name", "Email", 4, "Address", "Phone", 7, Tuple.Create("Mobile", 9, "Fax")); System.Console.WriteLine(neted_tup.Item2 + "\n" + neted_tup.Item5 + "\n" + neted_tup.Rest.Item1.Item1); Console.ReadLine(); } }
The result:
Name
Address
Mobile
You can see, we created seven items of int and string type. For the eighth item, Tuple.Create is used to create another tuple (nested tuple).
For accessing the nested tuple first item value, we used Rest property i.e.