开发者

Real world and efficient example of solving a problem in C# using Tuple class [duplicate]

This question already has answers here: 开发者_JAVA百科 Is Using .NET 4.0 Tuples in my C# Code a Poor Design Decision? (13 answers) What requirement was the tuple designed to solve? (13 answers) Closed 10 years ago.

We have been discussing Tuple and its various possible uses. In almost all cases it sounds like root of evil, it makes design worse, and generally calls for a specialized class instead of Tuple everywhere it could be used.

It is supposedly close to DB but its hard to find a use for Tuple in db persistant logic code too.

Has anyone any real world good sample of Tuple use , preferably closer to some real world domain model problem or any use actualy.


Tuple is said to have been introduced to .NET 4 because some programming languages (let's say, for example, IronPython or F#) support tuples as a core feature of the language, and the .NET BCL team wanted to provide a uniform tuple type for such languages (for language interoperability reasons, I guess):

The BCL team decided to work with the F# team to standardize on one tuple type for the framework so that every language could benefit from them. (in Eric Lippert's answer to the SO question, What problem was the tuple designed to solve? )

Tuple does not make much sense in the C# language IMHO, because C# doesn't support tuples and some associated language constructs very well; e.g. tuple "explosion": spreading a tuple over a function's parameters, or taking a function's return values (in the form of a Tuple) and spreading it over several local variables:

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

That being said, if you're writing 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 (such as in GetFoo above) is quick and convenient, but the return value doesn't have any inherent structure — you might do better with a struct or class type, if you're willing to take some additional time to define it.


There are many languages where it's legal to return multiple values (something like return (a, b) instead of using ref or out (these are considered another root of all evils) .) Tuple solves this problem.

Let's look at int Math.DivRem(int, int, out int). It could be refactored in Tuple<int, int> Math.DivRem(int, int). If you think that in this case instead of Tuple you should use special classes, well, for delegates the direction is the opposite: after creating hundred (tens in reality) of specialized delegates MS has created generic Action and Func and has begun using them.

(We will ignore that Tuple is a family of classes so using them is "slow". Premature optimization and all that beautiful things :-) )

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


the question is very open ended but I try anyhow.

  1. Tuples are great in a local context (inside methods) to shorten your algorithm, just like annonymous functions are.
  2. If you use good Types (meaningfull names) Tuple<Result, Message> are very readable only the Item1, Item2 things are not so nice
  3. this comes IMHO from functional programming (just like Func<_,_> and Action<_>) and can be very nice to have at times - Tuples are used to a great extent there - look at F# there tuples are the way to give multiple function-parameters instead of making a

function that returns a function that returns a value

for example :)

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


You don't usually use expose them as a part of a public interface, that's not what they were meant for (at least in C#). They just come in handy when you are too lazy to write your own class used only to solve a small problem.

I've found them especially convenient with Dictionaries. If you have a private dictionary which uses a key consisted of two or more properties, in order for it to work you need to make sure that you use compare-by-value semantics and implement hash code properly (or provide a custom Comparer). A Tuple already does this for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜