Using Tuple in C#

Introduction:

Tuple<T> is class in .NET. It is data structure which can hold elements of different data types. It can be used like any entity which can hold element of different data types. We can use Tuple instead of a class with properties. It is introduced in .NET Framework 4.0.

Syntax:

Tuple<T1,T2,T3,T4,T5,T6,T7,T8>

How to Create Tuple:

There are two way to create a Tuple.

1. Create object of Tuple<T> class.

static void Main(string[] args)

{

Tuple<string , string , decimal> employee = new Tuple<string , string, decimal>(“E001″,”Rajesh”,10000);

Console.WriteLine(“EmpId: ” + employee.Item1);

Console.WriteLine(“Name: ” + employee.Item2);

Console.WriteLine(“Salary: ” + employee.Item3);

Console.ReadLine();

}

Here, I have created object of Tuple class which stores employee information like employee id, name and salary. I have specified the types and passed values in constructor.

2. Helper Class:

.NET Framework provide a helper class to create object of Tuple class.

Example:

static void Main(string[] args)

{

var employee = Tuple.Create(“E001″,”Rajesh”,10000);

Console.WriteLine(“EmpId: ” + employee.Item1);

Console.WriteLine(“Name: ” + employee.Item2);

Console.WriteLine(“Salary: ” + employee.Item3);

Console.ReadLine();

}

Here, I have not specified data types. Create method is used to create instance of Tuple class and passed

the parameters empid, name and salary. Tuple can have maximum 8 elements.

How to use Tuple:

Tuple element can be accessed with Item<ElementNumber> properties.

Ex: Item1,Item2,Item3,Item4,Item5,Item6,Item7,Item8.

Eight element is last element and it is accessed using Rest property.

Example:

static void Main(string[] args)

{

var employee = Tuple.Create(“E001″,”Rajesh”,”Singh”,10000,”Male”,9983632000,”Bangalore”,”R&D”);

Console.WriteLine(“EmpId: ” + employee.Item1);

Console.WriteLine(“First Name: ” + employee.Item2);

Console.WriteLine(“Last Name: ” + employee.Item3);

Console.WriteLine(“Salary: ” + employee.Item4);

Console.WriteLine(“Gender: ” + employee.Item5);

Console.WriteLine(“Mobile: ” + employee.Item6);

Console.WriteLine(“City: ” + employee.Item7);

Console.WriteLine(“Department: ” + employee.Rest);

Console.ReadLine();

}

Output:

clip_image001[4]

How to use more than 8 elements in Tuple:

Sometimes there is requirement that we have to use more than 8 elements which is generally not allowed in Tuple. Max we can store 8 elements in Tuple. We have to use nested Tuple to hold more than 8 elements. We can use nested Tuple at any element. However, it is recommended to place nested tuple at the end of sequence. Last element is access by Rest property.

Example:

static void Main(string[] args)

{

var employee = Tuple.Create(“E001″,”Rajesh”,”Singh”,10000,”Male”,9983632000,”Bangalore”,

Tuple.Create(“D001″,”R&D”));

Console.WriteLine(“EmpId: ” + employee.Item1);

Console.WriteLine(“First Name: ” + employee.Item2);

Console.WriteLine(“Last Name: ” + employee.Item3);

Console.WriteLine(“Salary: ” + employee.Item4);

Console.WriteLine(“Gender: ” + employee.Item5);

Console.WriteLine(“Mobile: ” + employee.Item6);

Console.WriteLine(“City: ” + employee.Item7);

Console.WriteLine(“DepartmentId: ” + employee.Rest.Item1.Item1);

Console.WriteLine(“Department Name: ” + employee.Rest.Item1.Item2);

Console.ReadLine();

}

Output:

clip_image002[4]

How to pass multiple parameters to Method:

We can pass multiple parameters using following:

  • Ref
  • Out
  • Class object
  • Tuple
  • Array

Tuple is one of way to pass multiple parameters to method. I will explain here about Tuple.

public class Program

{

static void Main(string[] args)

{

var employee = Tuple.Create(“E001”, “Rajesh”, 10000);

GetTupleData(employee);

Console.ReadLine();

}

public static void GetTupleData(Tuple<string, string, int> employee)

{

Console.WriteLine(“EmpId: ” + employee.Item1);

Console.WriteLine(“First Name: ” + employee.Item2);

Console.WriteLine(“Salary: ” + employee.Item3);

}

}

}

I created instance of Tuple for employee properties holding Emp Id, Name and Salary. Passing employee object to GetTupleData() method and accessing object employee and printing on console.

Output:

clip_image003[4]

How to Return multiple values from Method using Tuple:

public class Program

{

static void Main(string[] args)

{

var employee = GetTupleData();

Console.WriteLine(“EmpId: ” + employee.Item1);

Console.WriteLine(“First Name: ” + employee.Item2);

Console.WriteLine(“Last Name: ” + employee.Item3);

Console.ReadLine();

}

public static Tuple<string,string,string> GetTupleData()

{

return Tuple.Create(“E001”, “Rajesh”, “Singh”);

}

}

I created instance of employee which is holding EmpId, First Name and Last Name. Employee object is returned from GetTupleData() method and printing EmpId, First Name and Last Name.

Output:

clip_image004[4]

Summary:

  • Tuple is used to hold element of different types without creating a class.
  • Tuple is reference type.
  • Tuple can hold max 8 elements. Nested Tuple can be used to hold more than 8 elements.
  • Tuple is used to return multiple values from a method.
  • Tuple is used to pass multiple values to method.
  • Tuple elements are access using properties (Item1,Item2,Item3,Item4,Item5,Item6, Item7,Rest).

Thanks for reading.

Happy Coding. Smile

Be the first to comment

Leave a Reply

Your email address will not be published.


*