开发者

Tuples in .net 4.0.When Should I use them

I have come across Tuples i开发者_StackOverflow社区n net 4.0. I have seen few example on msdn,however it's still not clear to me about the purpose of it and when to use them.

Is it the idea that if i want to create a collections of mix types I should use a tuple? Any clear examples out there I can relate to? When did you last use them?

Thanks for any suggestions


Tuples are just used on the coding process by a developer. If you want to return two informations instead of one, then you can use a Tuple for fast coding, but I recoment you make yourself a type that will contain both properties, with appropriate naming, and documentation.

Tuples are not used to mix types as you imagine. Tuples are used to make compositions of other types. e.g. a type that holds both an int and a string can be represented by a tuple: Tuple<int,string>.

Tuples exists in a lot of sizes, not only two.

I don't recommend using tuples in your final code, since their meaning is not clear.


Tuples can be used as multipart keys for dictionaries or grouping statements because being value types they understand equality. Avoid using them to move data around because they have poor language support in C# and named values (classes, structs) are better (simpler) than ordered values (tuples).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜