Accessing tuple elements
To access tuple elements use
Item8 properties. Only the properties with index number less or equal to tuple size are going to be available (i.e. one cannot access
Item3 property in
Comparing and sorting Tuples
Tuples can be compared based on their elements.
As an example, an enumerable whose elements are of type
Tuple can be sorted based on comparisons operators defined on a specified element:
Or to reverse the sort use:
Tuples are created using generic types
Tuple<T1,T2,T3,T4,T5,T6,T7,T8>. Each of the types represents a tuple containing 1 to 8 elements. Elements can be of different types.
Tuples can also be created using static
Tuple.Create methods. In this case, the types of the elements are inferred by the C# Compiler.
Since C# 7.0, Tuples can be easily created using ValueTuple.
Elements can be named for easier decomposition.
Return multiple values from a method
Tuples can be used to return multiple values from a method without using out parameters. In the following example
AddMultiply is used to return two values (sum, product).
Now C# 7.0 offers an alternative way to return multiple values from methods using value tuples More info about