Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Tuples
3.
Creating Tuples
4.
Accessing Tuple Elements
5.
Nested Tuples
6.
Tuple as a Method Parameter
7.
Tuple as a Return Type
8.
FAQs
8.1.
Is Tuple Reference Type?
8.2.
What should we do if we want to include more than eight elements in the Tuple?
8.3.
What is one limitation of Tuple?
8.4.
What is one usage of passing tuple in a method?
8.5.
When is tuple used as a return type? 
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Tuples in C#

Author Toohina Barua
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

The programming language C# (pronounced "See Sharp") is modern, object-oriented, and type-safe. C# allows developers to create a wide range of secure and reliable.NET applications.
.NET Framework 4.0 introduced the Tuple<T> class. A tuple is a data structure made up of a series of elements of various data types.
In this article, we will learn about Tuples in C# by looking at some coding examples. So let us dive in!

Recommended Topic -  singleton design pattern in c#

Tuples

The term "tuple" refers to a data structure of multiple parts. So a tuple is a data structure that makes it simple to represent a data set with multiple values that may or may not be related to one another. It first appeared in the .NET Framework 4.0. Tuples are commonly used when creating a data structure containing objects and their properties without creating a separate type.
Note: You can add elements from 1 to 8 to a tuple. The compiler will throw an error if you try to add more than eight elements.
Example: Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 MethodsType Conversionsclasses and objects in C#, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
HashMap in C#
Next article
C# Indexers
Live masterclass