The word Tuple means “a data structure which consists of the multiple parts”. So tuple is a data structure which gives you the easiest way to represent a data set which has multiple values that may/may not be related to each other. It introduced in .NET Framework 4.0. In tuple, you can add elements from 1 to 8. If you try to add elements greater than eight, then the compiler will throw an error. Tuples are generally used when you want to create a data structure which contains objects with their properties and you don’t want to create a separate type for that.
Features of Tuples:
- It allows us to represent multiple data into a single data set.
- It allows us to create, manipulate, and access data set.
- It return multiple values from a method without using out parameter.
- It can also store duplicate elements.
- It allows us to pass multiple values to a method with the help of single parameters.
What is the need of C# Tuples?
Before tuples, we have three ways to return more than one value from the method in C# which are Using Class or Struct types, Out parameters and Anonymous types which returned through a Dynamic Return Type. But after Tuples, it becomes easy to represent a single set of data.
For another reason, just imagine a case where you want to store the details of an Employee in just one entity, like Name, EmpID, Blood Group, Contact Number. Now the most common way that comes to the mind is to create a data structure which would take the required fields. This is where Tuples come into play. With Tuples, there is no need to create any separate data structure. Instead, for this case, you can use Tuple<T1, T2, T3, T4>.
The most common data structures like Array, List, etc. are only of a specific type and can store infinite elements. But Tuples are able to store a limited number of elements i.e 8 and can be of any type.
Creating a Tuple
In C#, there are mainly 2 ways to create the tuple which are as follows:
- Using Constructor of Tuple Class: You can create a tuple by using the constructor which is provided by Tuple<T> class. Where you can store elements starting from one to eight with their type. But you are not allowed to store elements greater than eight in a tuple. If you try to do so then the compiler will throw an error.
Syntax:
// Constructor for single elements
Tuple <T1>(T1)
// Constructor for two elements
Tuple <T1, T2>(T1, T2)
.
.
.
// Constructor for eight elements
Tuple <T1, T2, T3, T4, T5, T6, T7, TRest>(T1, T2, T3, T4, T5, T6, T7, TRest)
Example:
using System;
public class GFG{
static public void Main (){
Tuple< string >My_Tuple1 = new Tuple< string >( "GeeksforGeeks" );
Tuple< string , string , int >My_Tuple2 = new Tuple< string , string , int >( "Romil" , "Python" , 29);
Tuple< int , int , int , int , int , int , int , Tuple< int >>My_Tuple3 = new Tuple< int , int , int , int , int , int , int , Tuple< int >>(1,2,3,4,5,6,7, new Tuple< int >(8));
}
}
|
- Using Create Method: When we use the tuple constructor to create a tuple we need to provide the type of each element stored in the tuple which makes your code cumbersome. So, C# provides another class that is Tuple class which contains the static methods for creating tuple object without providing the type of each element.
Syntax:
// Method for 1-tuple
Create(T1)
// Method for 2-tuple
Create(T1, T2)
.
.
.
// Method for 8-tuple
Create(T1, T2, T3, T4, T5, T6, T7, T8)
Example:
using System;
public class GFG {
static public void Main()
{
var My_Tuple1 = Tuple.Create( "GeeksforGeeks" );
var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
var My_Tuple3 = Tuple.Create(13, "Geeks" , 67,
89.90, 'g' , 39939, "geek" , 10);
}
}
|
Accessing a Tuple
You can access the elements of a tuple by using Item<elementNumber> property, here elementNumber is from 1 to 7 like Item1, Item 2, Item3, Item4, Item5, Item6, Item 7, etc. and the last element of 8-tuple is accessible by using Rest property. As shown in below example:
Example:
using System;
public class GFG {
static public void Main()
{
var My_Tuple1 = Tuple.Create( "GeeksforGeeks" );
Console.WriteLine( "Element of My_Tuple1: " + My_Tuple1.Item1);
Console.WriteLine();
var My_Tuple2 = Tuple.Create(12, 30, 40, 50);
Console.WriteLine( "Element of My_Tuple2: " + My_Tuple2.Item1);
Console.WriteLine( "Element of My_Tuple2: " + My_Tuple2.Item2);
Console.WriteLine( "Element of My_Tuple2: " + My_Tuple2.Item3);
Console.WriteLine( "Element of My_Tuple2: " + My_Tuple2.Item4);
Console.WriteLine();
var My_Tuple3 = Tuple.Create(13, "Geeks" ,
67, 89.90, 'g' , 39939, "geek" , 10);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item1);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item2);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item3);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item4);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item5);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item6);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Item7);
Console.WriteLine( "Element of My_Tuple3: " + My_Tuple3.Rest);
}
}
|
Output:
Element of My_Tuple1: GeeksforGeeks
Element of My_Tuple2: 12
Element of My_Tuple2: 30
Element of My_Tuple2: 40
Element of My_Tuple2: 50
Element of My_Tuple3: 13
Element of My_Tuple3: Geeks
Element of My_Tuple3: 67
Element of My_Tuple3: 89.9
Element of My_Tuple3: g
Element of My_Tuple3: 39939
Element of My_Tuple3: geek
Element of My_Tuple3: (10)
Nested Tuples
In C#, you are allowed to create a tuple into another tuple. You can use nested tuples when you want to add more than eight elements in the same tuple. The nested tuple is accessible by using the Rest property as shown in Example 1. You are allowed to add a nested tuple anywhere in the sequence, but it is recommended that you can place nested tuple at the end of the sequence so that they can easily access from the Rest property. If you place nested tuple other than the last place, then the tuple is accessible according to Item<elementNumber> property as shown in example 2.
Example 1:
using System;
public class GFG{
static public void Main ()
{
var My_Tuple = Tuple.Create(13, "Geeks" , 67, 89.90,
'g' , 39939, "geek" , Tuple.Create(12, 30, 40, 50));
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item1);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item2);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item3);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item4);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item5);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item6);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item7);
Console.WriteLine( "Element of Nested tuple: " +My_Tuple.Rest);
Console.WriteLine( "Element of Nested tuple: " +My_Tuple.Rest.Item1.Item1);
Console.WriteLine( "Element of Nested tuple: " +My_Tuple.Rest.Item1.Item2);
Console.WriteLine( "Element of Nested tuple: " +My_Tuple.Rest.Item1.Item3);
Console.WriteLine( "Element of Nested tuple: " +My_Tuple.Rest.Item1.Item4);
}
}
|
Output:
Element of My_Tuple: 13
Element of My_Tuple: Geeks
Element of My_Tuple: 67
Element of My_Tuple: 89.9
Element of My_Tuple: g
Element of My_Tuple: 39939
Element of My_Tuple: geek
Element of Nested tuple: ((12, 30, 40, 50))
Element of Nested tuple: 12
Element of Nested tuple: 30
Element of Nested tuple: 40
Element of Nested tuple: 50
Example 2:
using System;
public class GFG{
static public void Main ()
{
var My_Tuple = Tuple.Create(13, Tuple.Create(12, 30, 40,
50),67, 89.90, 'g' , 39939, 123, "geeks" );
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item1);
Console.WriteLine( "Element of Nested Tuple: " +My_Tuple.Item2.Item1);
Console.WriteLine( "Element of Nested Tuple: " +My_Tuple.Item2.Item2);
Console.WriteLine( "Element of Nested Tuple: " +My_Tuple.Item2.Item3);
Console.WriteLine( "Element of Nested Tuple: " +My_Tuple.Item2.Item4);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item3);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item4);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item5);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item6);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Item7);
Console.WriteLine( "Element of My_Tuple: " +My_Tuple.Rest);
}
}
|
Output:
Element of My_Tuple: 13
Element of Nested Tuple: 12
Element of Nested Tuple: 30
Element of Nested Tuple: 40
Element of Nested Tuple: 50
Element of My_Tuple: 67
Element of My_Tuple: 89.9
Element of My_Tuple: g
Element of My_Tuple: 39939
Element of My_Tuple: 123
Element of My_Tuple: (geeks)
Tuple as a Method Parameter
In C#, you are allowed to pass a tuple as a method parameter as shown in the below example. Here we pass a tuple named mytuple in the PrintTheTuple() method and the elements of the tuple are accessed by using Item<elementNumber> property.
Example:
using System;
public class GFG{
static public void Main ()
{
var mytuple = Tuple.Create( "GeeksforGeeks" , 123, 90.8);
PrintTheTuple(mytuple);
}
static void PrintTheTuple(Tuple< string , int , double >mytuple)
{
Console.WriteLine( "Element: " +mytuple.Item1);
Console.WriteLine( "Element: " +mytuple.Item2);
Console.WriteLine( "Element: " +mytuple.Item3);
}
}
|
Output:
Element: GeeksforGeeks
Element: 123
Element: 90.8
Tuple as a Return Type
In C#, methods are allowed to use tuple as a return type. Or in other words a method can return a tuple as shown in the below example:
Example:
using System;
public class GFG{
static public void Main ()
{
var mytuple = PrintTuple();
Console.WriteLine(mytuple.Item1);
Console.WriteLine(mytuple.Item2);
Console.WriteLine(mytuple.Item3);
}
static Tuple< string , string , string >PrintTuple()
{
return Tuple.Create( "Geeks" , "For" , "Geeks" );
}
}
|
Output:
Geeks
For
Geeks
Limitation of Tuple:
- It is of reference type not of value type.
- It is limited to eight elements. Means you cannot store more than eight elements without nested tuple.
- These are only accessed by using Item<elementNumber> property.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Apr, 2019
Like Article
Save Article