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.
Methods to Create Tuple
In C#, there are mainly 2 ways to create the tuple which are as follows:
Using Constructor of Tuple Class
Using Create Method
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)
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)
How to Access the tuple?
A tuple elements can be accessed with Item<elementNumber> properties, e.g., Item1, Item2, Item3, and so on up to Item7 property. The Item1 property returns the first element, Item2 returns the second element, and so on. The last element (the 8th element) will be returned using the Rest property.
using System;
public class Program
{
public static void Main()
{
var person = Tuple.Create(1, "The Tech ", "Platform");
Console.WriteLine(person.Item1); // returns 1
Console.WriteLine(person.Item2); // returns "The Tech"
Console.WriteLine(person.Item3); // returns "Platform"
var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
Console.WriteLine(numbers.Item1); // returns 1
Console.WriteLine(numbers.Item2); // returns 2
Console.WriteLine(numbers.Item3); // returns 3
Console.WriteLine(numbers.Item4); // returns 4
Console.WriteLine(numbers.Item5); // returns 5
Console.WriteLine(numbers.Item6); // returns 6
Console.WriteLine(numbers.Item7); // returns 7
Console.WriteLine(numbers.Rest); // returns (8)
Console.WriteLine(numbers.Rest.Item1); // returns 8
}
}
Output:
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.
Advantages of Tuple
Tuples offer the following advantages −
Tuples are fined size in nature i.e. we can’t add/delete elements to/from a tuple.
We can search any element in a tuple.
Tuples are faster than lists, because they have a constant set of values.
Tuples can be used as dictionary keys, because they contain immutable values like strings, numbers, etc.
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.
The Tech Platform
Comments