The real world and an effective example of solving a problem in C # using the Tuple class

We discussed Tuple and its various possible uses. In almost all cases, this sounds like the root of evil, it makes design even worse, and it usually calls for the use of a specialized class instead of the Tuple around the world that it can use.

It is supposed to be close to the database, but it is hard to find for Tuple in db-compatible logic code.

Does anyone in the real world have a good example of using Tuple, preferably closer to some real-world domain model problem or use some kind of use.

+4
source share
4 answers

Tuple is said to have been introduced in .NET 4 because some programming languages ​​(say, for example IronPython or F #) support tuples as the main function of the language, and the .NET BCL team would like to provide I would suggest that this type of tuple for such languages ​​(for language interoperability ):

The BCL team decided to work with the F # team to standardize one type of tuple for the framework so that each language could benefit from it. (Eric Lippert answers the SO question, What is the problem the tuple was supposed to solve? )

Tuple doesn't make much sense in C # IMHO because C # really doesn't support tuples and some related language constructs; for example, an β€œexplosion” tuple: propagating a tuple according to the parameters of a function or returning the values ​​of a function (in the form of a Tuple ) and distributing it over several local variables:

 Tuple<int,int> GetFoo() { … } // vv int (a, b) = GetFoo(); // C# doesn't support this tuple-related syntax. 

If you write only C # code, you can usually find "better", more structured solutions than using Tuple . For example, using tuples to return more than one value from a method (for example, in GetFoo above) is quick and convenient, but the return value does not have any structure of its own - you might be better off with a struct or class type if you want to take some extra time to determine it.

+4
source

There are many languages ​​where it is legal to return multiple values ​​(for example, return (a, b) instead of using ref or out (they are considered another root of all evils).) Tuple solves this problem.

Let's look at int Math.DivRem(int, int, out int) . It can be reorganized into Tuple<int, int> Math.DivRem(int, int) . If you think that in this case you should use special classes instead of Tuple, then the direction is different for delegates: after creating hundreds (tens of actually) specialized delegates, MS created common Action and Func and has started using them.

(We will ignore that Tuple is a family of classes, so their use is β€œslow.” Premature optimization and all these nice things :-))

I will add that Tuple solves the problem of writing short code blocks here on SO :-)

+3
source

the question is very open, but I still try.

  • Tuples are excellent in local context (inside methods) to shorten your algorithm, as well as anonymous functions.
  • If you use the good types (meaningful names) of Tuple<Result, Message> , they are very readable, but things Item1 , Item2 not so good
  • this comes from IMHO from functional programming (just like Func<_,_> and Action<_> ) and it can be very nice to have from time to time - tuples are used to a large extent - look at F #, there tuples are way to give multiple parameter functions instead of creating

which returns a function that returns a value

eg:)

Just don't use them in your Big Picture (you can call it your design), but for your local implementations they are great

+2
source

Usually you do not use them as part of an open interface, which is not what they are intended for (at least in C #). They just come in handy when you're too lazy to write your own class, used only to solve a small problem.

I found them especially convenient with Dictionaries . If you have a private dictionary that uses a key consisting of two or more properties, for it to work, you need to make sure that you use semantics in different ways and correctly implement the hash code (or provide a custom Comparer ). A Tuple already doing this for you.

0
source

All Articles