Creating Tuples
The following example generates a three-element tuple:
Tuple<int, string, string> person = new Tuple <int, string, string>(1, "Coding", "Ninjas");
In the preceding example, we created a Tuple instance that contains a person's record. We gave each element a type and passed values to the constructor. It's time-consuming to specify the type of each element. As shown below, C# includes a static helper class Tuple that returns an instance of the Tuple<T> without specifying the type of each element.
var person = Tuple.Create(1, "Coding", "Ninjas");
A tuple can only contain up to eight elements. When you try to include more than eight elements, you get a compiler error.
var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
Accessing Tuple Elements
Item<elementNumber> properties can be used to access tuple elements, such as Item1, Item2, Item3, and so on up to Item7. The first element is returned by the Item1 property, the second by Item2, and so on. The “Rest” property will be used to return the last element (the 8th element).
var person = Tuple.Create(1, "Coding", "Ninjas");
person.Item1;
person.Item2;
person.Item3;
var numbers = Tuple.Create(1, “two”, 3, "Four", 5, "Six", 7, 8);
numbers.Item1;
numbers.Item2;
numbers.Item3;
numbers.Item4;
numbers.Item5;
numbers.Item6;
numbers.Item7;
numbers.Rest;
numbers.Rest.Item1;
Output:
1
Coding
Ninjas
1
two
3
Four
5
Six
7
(8)
8
The nested tuple, which you can access using the Rest property, stores the element at the 8th position.
Nested Tuples
You can nest another tuple object as the eighth element in a tuple if you want to include more than eight elements. The “Rest” property can be used to access the last nested tuple. Use the Rest.Item1.Item<elelementNumber> property to get to the element of the nested tuple.
using System;
public class Program
{
public static void Main()
{
var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9, 10, 11, 12, 13));
Console.WriteLine(numbers.Item1);
Console.WriteLine(numbers.Item7);
Console.WriteLine(numbers.Rest.Item1); Console.WriteLine(numbers.Rest.Item1.Item1);
Console.WriteLine(numbers.Rest.Item1.Item2);
}
}
Output:
1
7
(8, 9, 10, 11, 12, 13)
8
9
Tuple as a Method Parameter
As shown in the example below, you can pass a tuple as a method parameter in C#. The DisplayCompanyInfo() method is passed a tuple named company, and the elements of the tuple are accessed using the Item<elementNumber> property.
static void
Main(string[] args) {
var company = Tuple.Create(1, "Coding", "Ninjas");
DisplayCompanyInfo(company);
}
static void DisplayCompanyInfo(Tuple<int,string,string> company) {
Console.WriteLine($"Number = { company.Item1}"); Console.WriteLine($"Company Name = {company.Item2} { company.Item3}");
}
Output:
1
Coding Ninjas
Tuple as a Return Type
Tuple can be used as a return type in C# methods. Alternatively, a method can return a tuple, as shown in the example below:
static void Main(string[] args) {
var company= GetCompany();
}
static Tuple<int, string, string> GetCompany() {
return Tuple.Create(1, "Coding", "Ninjas");
}
Also see, Ienumerable vs Iqueryable
FAQs
Is Tuple Reference Type?
The Tuple is not a value type, but rather a reference type. It allocates on the heap, which may lead to CPU-intensive operations.
What should we do if we want to include more than eight elements in the Tuple?
The Tuple can only contain eight elements. If you need to store more elements, you'll need to use nested tuples. However, this could lead to ambiguity.
What is one limitation of Tuple?
The Tuple elements are accessible through properties with the name pattern Item<elementNumber>, which is illogical.
What is one usage of passing tuple in a method?
When you want to use a single parameter to pass multiple values to a method.
When is tuple used as a return type?
When you don't want to use ref or out parameters and want to return multiple values from a method.
Conclusion
In this article, we discussed the theoretical and practical implementation of Tuples in C#.
We hope that this blog has helped you enhance your knowledge regarding the Tuples in C# and if you would like to learn more about Methods, Type Conversions, classes and objects in C#, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!