Summary: in this tutorial, you’ll learn how to use C# tuples to represent a set of values as a single unit.
Introduction to the C# tuples #
A tuple allows you to represent a set of values as a single unit. The type of tuples is System.ValueTuple.
Creating tuples & accessing elements #
The following shows how to create a new tuple type:
using static System.Console;
var person = ("John Doe", 22);
WriteLine(person);Output:
(John Doe, 22)In this example, we define a tuple named person with two fields. The first field is a string that represents the person’s name and the second field is an integer that represents the person’s age.
To access a field, you use the dot syntax:
tuple.fieldBecause fields of the person tuple have no names, C# assigns default field names to them as Item1, Item2, etc. For example:
using static System.Console;
var person = ("John Doe", 22);
WriteLine($"Name: {person.Item1}, Age: {person.Item2}");Output:
Name: John Doe, Age: 22To assign names to the fields of the tuple, you can use the following syntax:
using static System.Console;
var person = (Name: "John Doe", Age: 22) ;
WriteLine($"Name: {person.Name}, Age: {person.Age}");In this example, we assign names to the fields of the person tuple and access their values via the field names.
Deconstructing tuple elements #
Deconstructing a tuple means assigning elements of the tuple to multiple variables. To deconstruct elements of a tuple, you use the = operator. For example:
using static System.Console;
var point = (10,20);
var (x,y) = point; // deconstructing a tuple
WriteLine($"x:{x}, y:{y}");Output:
x:10, y:20In this example, we assign the first and second elements of a tuple to the x and y variables.
You can also specify the types of variables explicitly when deconstructing a tuple:
using static System.Console;
var point = (10,20);
(int x,int y) = point; // deconstructing a tuple
WriteLine($"x:{x}, y:{y}");Returning tuples #
Tuples are useful when you want to return multiple values from a method. For example:
using static System.Console;
static (int sum, int product) Calculate(int x, int y)
{
return (x + y, x * y);
}
var result = Calculate(20, 30);
WriteLine(result.sum); // output: 50
WriteLine(result.product); // output: 600In this example, the method Calculate returns a named tuple with two elements: sum and product. When calling the method, we can assign the returned tuple to a variable result and access its elements using dot notation.
Note that you can deconstruct the tuple returned by the method like this:
using static System.Console;
static (int sum, int product) Calculate(int x, int y)
{
return (x + y, x * y);
}
var (sum,product) = Calculate(20, 30);
WriteLine(sum); // output: 50
WriteLine(product); // output: 600Comparing Tuples #
Tuples support the == and != operators. Two tuples are comparable when they comply with the following conditions:
- Both tuples have the same number of elements.
- The corresponding tuple elements are comparable using the
==and!=operators.
For example, the following example will result in an error because the number of elements is different:
var point2D = (10,20);
var point3D = (10,20,30);
point2D == point3D; // ERRORThe following example demonstrates that two tuples are equal:
using static System.Console;
var point1 = (10,20,30);
var point2 = (10, 20,30);
var result = point1 == point2;
WriteLine($"Result:{result}");Output:
Result:TrueSystem.ValueTuple vs. System.Tuple #
So far, you have learned the tuples that belong to the System.ValueTuple type. They are completely different from tuples backed by the System.Tuple types.
The following table illustrates the main differences:
| System.ValueTupe | System.Tuple |
|---|---|
| Tuples are value types | Tuples are reference types |
| Tuples are mutable | Tuples are immutable |
| Data members are fields | Data members are properties |
Summary #
- Use a tuple to represent a set of values as a single variable.
- Do use tuples to make your code more readable and expressive.
Thank you for your feedback!