Starting with C # 7.0, you can use tuples of values. C # 7.0 introduces not only a new type, but also a simplified syntax for tuple types, as well as for tuple values. The tuple type is simply written as a list of types enclosed in braces:
(string, int, double)
The corresponding items are called Item1 , Item2 , Item2 . You can also specify additional aliases. These aliases are syntactic sugar only (C # compiler trick); tuples are still based on the invariant (but common) System.ValueTuple<T1, T2, ...> struct .
(string name, int count, double magnitude)
Tuple values ββhave similar syntax except that you specify expressions instead of types
("test", 7, x + 5.91)
or with aliases
(name: "test", count: 7, magnitude: x + 5.91)
Example with params array:
public static void MyFunction(params (string Key, object Value)[] pairs) { foreach (var pair in pairs) { Console.WriteLine($"{pair.Key} = {pair.Value}"); } }
It is also possible to deconstruct a tuple like this
var (key, value) = pair; Console.WriteLine($"{key} = {value}");
This extracts the tuple elements into two separate variables, key and value .
Now you can easily call MyFunction with a different number of arguments:
MyFunction(("a", 1), ("b", 2), ("c", 3));
This allows us to do things like
DrawLine((0, 0), (10, 0), (10, 10), (0, 10), (0, 0));
See: New Features in C # 7.0