In c#, Tuple is a data structure, and it is useful to store the sequence of elements of different data types. Using tuple, we can return more than one value from the methods as a single set of data.
Tuples were introduced in .NET Framework 4.0, and the syntax of tuples will be as shown below.
In .NET Framework, the Tuple will directly support only 1 to 7 elements. If we want to create a tuple object to hold 8 or more elements, we need to nest the tuple objects in the TRest property.
Following is the example of creating a tuple object (user) with 4 elements.
If you observe the above declarations, we created an instance of the Tuple class by specifying the type for each element to hold the details of the user in a single object.
As discussed, the tuple object we create with the Tuple class instance will directly support only 1 to 7 elements. If we define 8 or more elements, we will get a compile-time error like “The last element of an eight element Tuple must be a Tuple”.
Following is the invalid way of defining the tuple objects in c#.
If you observe the above tuple object declarations, every time we are passing the values to the tuple object by specifying the type for each element, and it’s cumbersome. So, to avoid sending the type for each element, we need to create a tuple object by using the Tuple class helper method “Create”.
Following is the example of creating a tuple object using the Tuple class static method “Create” without specifying the types for each element.
If you observe the above code, we created a tuple object (numbers) using the Create method with 8 elements. The Create method will directly support the creation of a tuple object from 1 to 8 elements.
If we create a tuple object with an instance of the Tuple class, it will support only 1 to 7 elements. If we create with Create method of Tuple class, it will support 1 to 8 elements.
In c#, we can access the tuple object elements with Item<positionNumber> properties. For example, the first element can be accessed like Item1, the second element can be accessed like Item2, and so on based on our requirements.
Following is the example of accessing the tuple object elements in c#.
If you observe the above code snippet, we are accessing the 8th position of an element using the Rest property because the 8th position is for nested tuples, and that can be accessed by using the Rest property.
In c#, tuples are commonly useful in the following scenarios.
Following is the example of creating and accessing the tuple object values in c#.
When we execute the above program, we will get the result as shown below.
As discussed, the Tuple will support only 8 elements. If we want to include more than 8 elements in a tuple, we need to nest another tuple object at the 8th position, and it can access by using the Rest property.
Following is the example of creating and accessing the nested tuples in c#.
If you observe the above example, we included more than 8 elements in a tuple object (numbers) by nesting another tuple object at the 8th element. We are accessing nested tuple elements by using the Rest property.
When we execute the above program, we will get the result as shown below.
In c#, the nested tuple object can include anywhere in the sequence, but it is recommended to place the nested tuple at the end of the sequence so that it can access by using the Rest property.
Following is the example of including the nested tuple object at the 4th position of sequence in the defined tuple object.
If you observe the above example, we included nested tuple object at 4th position of sequence and accessing nested tuple object values using Item4 element.
When we execute the above program, we will get the result as shown below.
This is how we can include more than 8 elements in the tuple object by nesting another tuple object at the 8th element based on our requirements.
In c#, if we accept Tuple as a method parameter, then we can send multiple values to the method with a single parameter.
Following is the example of using a tuple as a method parameter in c#.
If you observe the above example, we send tuple object (user) as a parameter to the GetUserDetails() method.
When you execute the above program, you will get the result as shown below.
Using a tuple, we can send multiple values as a single parameter to the method based on our requirements.
In c#, we can use a tuple as a method return type to return the multiple values without using ref or out parameters.
Following is the example of using a tuple as the return type of method to return multiple values in c#.
If you observe the above example, we created a method GetUserDetails() with a return type like a tuple object to return multiple values.
When we execute the above program, we will get the result as shown below.
The following are the limitations of tuple object in c#.
To overcome the limitations of Tuple, the ValueTuple has been introduced in C# 7.0. In the next chapters, we will learn about ValueTuple in detail with examples.