Tuples
Tuples are simple types with a lightweight syntax that can typically be used when you want to return multiple values from a function without defining an explicit type or without using out
or ref
parameters or when you want to pass multiple values to a method as a single object.
This aspect represents the key difference between anonymous types and tuples. The former is meant for use within the scope of a single method and cannot be passed as an argument or returned from a method. The latter are intended for this exact purpose.
In C#, there are two kinds of tuples:
- Reference tuples, represented by the
System.Tuple
class - Value tuples, represented by the
System.ValueTuple
structure
In the next subsection, we will look at both of these types.
The Tuple class
Reference tuples were introduced in .NET Framework 4.0. The generic class, System.Tuple
, can hold up to eight values of different types. Should you need tuples with more than eight values, you...