What is the real use of the tuple?

Can someone explain what the use of Tuple is?

+4
source share
1 answer

You might think that tuples are a bit like anonymous types, but without names - and with the ability to specify return types, etc. They are useful when you want to use a special data type with several values, but you want to be able to specify this as the return type of the method.

For example, int.TryParse could have a signature

 static Tuple<int, bool> TryParse(string text) 

Basically you want to return int and a bool . An existing signature uses the out parameter to circumvent the fact that you can only return one value. Baskets are another option. Similarly, KeyValuePair<TKey, TValue> is just a pair of values.

Personally, I would like to see another option: a short way to achieve the semantics of anonymous types (immutability, named properties, equality, etc.), but with a name.

+9
source

Source: https://habr.com/ru/post/1311176/


All Articles