Tuples
Explore how to use tuples in C# to group multiple values efficiently. Learn to create named tuples, deconstruct them into variables, and employ tuples to return or pass multiple values in methods for clearer, maintainable code.
Tuples provide a convenient way to group and work with multiple values.
A tuple is a set of values enclosed in parentheses:
(int, int) someTuple = (4, 2);
We declare a variable someTuple of type (int, int), holding two integer values 4 and 2.
The order in which values are placed matters. We can also declare the type implicitly by using the var keyword:
var someTuple = (4, 2);
Just like with primitive types, using var lets the compiler infer the (int, int) tuple type based on the assigned values, keeping our syntax concise.
Accessing tuple contents
In the example above, we created a tuple that holds two values. By default, ...