If you have a method that you want to return multiple values from it without using out the ref parameters, a tuple is an excellent feature for such a use case.
In this tutorial, we will show you how to use tuples as method return types in C# without defining out the parameters.
C# Tuple Basics
There are two main ways of defining a tuple in C#. The first is an explicit method where we use the “Tuple” class and the second is an implicit declaration using the literal format.
1. Explicit Declaration of Tuple
The following shows how to declare a tuple in C# using the “Tuple” class. This method is also known as explicit declaration.
In the given example, we declare a tuple called “t” that stores, an int, a string, and a Boolean element in a single entity.
2. Implicit Declaration of Tuple
We can also use the literal format to declare a tuple in C# as shown in the following example code:
Using the literal declaration is more concise and readable as it follows the general format of variable declaration.
Accessing the Tuple Elements
We can access the tuple elements by referencing their position in the tuple. Take the following code for example:
using System.IO;
class Program
{
static void Main()
{
Tuple<int, string, bool> t = new Tuple<int, string, bool>(1, "Linuxhint", true);
Console.WriteLine(t.Item1);
Console.WriteLine(t.Item2);
Console.WriteLine(t.Item3);
}
}
In the previous example, we demonstrate how to access the tuple elements by referencing their position in the tuple. The resulting output is as follows:
Linuxhint
True
If we define a tuple using the literal format, we can access the tuple elements using their names as demonstrated in the following example:
using System.IO;
class Program
{
static void Main()
{
var t = (Id: 1, Site: "Linuxhint", IsActive: true);
Console.WriteLine(t.Id);
Console.WriteLine(t.Site);
Console.WriteLine(t.IsActive);
}
}
You might notice that in this example, each element in the tuple has a unique name which allows us to quickly identify the value. This can help increase the code readability and avoid the name collisions.
Using Tuples as Method Return Type
Let us get to the meat of the tutorial and learn how to use a tuple as a method return type.
We do this by specifying the tuple type as the return type or using a tuple literal.
Let us look at the usage of the first method.
class Program
{
static void Main()
{
Tuple<int, string, int> carDetails = GetCar();
Console.WriteLine($"ID: {carDetails.Item1}");
Console.WriteLine($"Model: {carDetails.Item2}");
Console.WriteLine($"Mileage: {carDetails.Item3}");
}
public static Tuple<int, string, int> GetCar()
{
int id = 1;
string model = "Mercedes-Benz";
int mileage = 1000;
return new Tuple<int, string, int>(id, model, mileage);
}
}
In the given example, we define a method called GetCar() that contains three properties: id, model, and mileage.
NOTE: The method returns a tuple as defined in the explicit style. We can then call the method and access the tuple items as shown in the previous example.
The second technique is the literal tuple format. This allows us to define the method that should return a tuple as follows:
{
int id = 1;
string model = "Mercedes-Benz";
int mileage = 1000;
return (id, model, mileage);
}
In this case, you can access the tuple elements as we did in the previous steps.
Tuple Unpacking/Destruction
Tuple destruction refers to the process of unpacking a tuple and assigning its elements to a separate variable as shown in the following example:
class Program
{
static void Main()
{
(int id, string model, int mileage) = GetCar();
Console.WriteLine($"ID: {id}");
Console.WriteLine($"Model: {model}");
Console.WriteLine($"Mileage: {mileage}");
}
public static (int, string, int) GetCar()
{
int id = 1;
string model = "Mercedes-Benz";
int mileage = 1000;
return (id, model, mileage);
}
}
In the given example, the “GetCar” method returns a tuple that contains an int, a string, and another int. During the method call, we unpack the tuple into individual variables which we can access.
To unpack the tuple, we pass the elements that we wish to access inside the pair of parentheses before calling the method.
Conclusion
In this tutorial, we learned all about tuples in C#. We discovered how to define the tuples, access the tuple elements, use the tuples as method return types, and tuple destruction.