What is Tuple in C#
C# has a feature called Tuple which allows you to group together multiple values of different types into a single object. Tuples are immutable, meaning that once you create a tuple, you cannot modify its contents.
In this blog, we will explore the Tuple feature in C# and how to use it with code samples.
What are Tuples?
A Tuple is an ordered collection of elements of different types. It is similar to an array or a list, but unlike arrays or lists, Tuples are immutable. This means that once a Tuple is created, its elements cannot be modified. Tuples are defined using parentheses and commas to separate the elements, like this:
(int, string) myTuple = (10, "Hello World");
This creates a Tuple with two elements: an integer with the value of 10 and a string with the value of “Hello World”. Tuples can also be named using the following syntax:
(int id, string name) person = (1, "Rajan Arora");
This creates a Tuple with two named elements: an integer with the name “id” and a string with the name “name”.
How to use Tuples in C#
To create a Tuple in C#, you can use the new tuple syntax which uses round brackets () to enclose the values. Here is an example of creating a Tuple with two elements:
var tuple = (10, "Hello");
In this example, we have created a Tuple with two elements: an integer value of 10 and a string value of “Hello”. You can also name the elements of the Tuple by using the colon : and the element name. Here is an example of creating a Tuple with named elements:
var tuple = (Age: 20, Name: "Rajan Arora");
Accessing Tuple Elements To access the elements of a Tuple, you can use the dot notation followed by the element name or use the deconstruction syntax to extract the elements into separate variables. Here is an example of accessing Tuple elements using dot notation:
Console.WriteLine(tuple.Age); // Output: 20
Console.WriteLine(tuple.Name); // Output: Rajan Arora
Here is an example of deconstructing a Tuple into separate variables:
var (age, name) = tuple;
Console.WriteLine(age); // Output: 20
Console.WriteLine(name); // Output: Rajan Arora
Tuples with Methods You can also use Tuples as return types for methods in C#. This allows you to return multiple values from a single method. Here is an example of a method that returns a Tuple:
public static (int, string) GetTuple()
{
return (10, "Hello");
}
You can then call this method and access the Tuple elements like this:
var tuple = GetTuple();
Console.WriteLine(tuple.Item1); // Output: 10
Console.WriteLine(tuple.Item2); // Output: Hello
Tuples in Collections Tuples can also be used in collections such as arrays, lists, and dictionaries. Here is an example of creating a list of Tuples:
var tuples = new List<(int, string)>
{
(10, "John"),
(20, "Mary"),
(30, "Alice")
};
You can then iterate over the list and access the Tuple elements:
foreach (var tuple in tuples)
{
Console.WriteLine(tuple.Item1); // Output: 10, 20, 30
Console.WriteLine(tuple.Item2); // Output: John, Mary, Alice
}
Alternatively, you can use deconstruction to extract the Tuple elements into separate variables:
foreach (var (age, name) in tuples)
{
Console.WriteLine(age); // Output: 10, 20, 30
Console.WriteLine(name); // Output: John, Mary, Alice
}
In conclusion, Tuples are a powerful new feature introduced in C# 10 that allows you to group together multiple values of different types into a single object. Tuples are immutable and can be accessed using dot notation or deconstruction. Tuples can also be used as return types for methods and in collections such as arrays and lists.
Thanks for reading the article. Please let me know your thoughts about this feature. Do let me know where you are going to use this feature in the comment section or mail me @ [email protected].