在C#中使用一对(三等等)值作为一个值的最佳方式是什么? [英] What's the best way of using a pair (triple, etc) of values as one value in C#?

查看:122
本文介绍了在C#中使用一对(三等等)值作为一个值的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的想法是:

 字典< Pair< string,int>,对象> 

  Dictionary< Triple< string,int,int>,对象> 

是否有内置类型,如Pair或Triple?或者实现它的最佳方法是什么?



更新答案中描述了一些通用的元组实现,但是用作元组字典中的键,您应该额外验证哈希码的正确计算。另一个问题的更多信息a>。



更新2 我想这也值得一提,当您在字典中使用一些值作为键时,应该是

解决方案

我在C#中实现了一个元组库。请访问 http://www.adventuresinsoftware.com/generics/ ,然后点击元组链接。


That is, I'd like to have a tuple of values.

The use case on my mind:

Dictionary<Pair<string, int>, object>

or

Dictionary<Triple<string, int, int>, object>

Are there built-in types like Pair or Triple? Or what's the best way of implementing it?

Update There are some general-purpose tuples implementations described in the answers, but for tuples used as keys in dictionaries you should additionaly verify correct calculation of the hash code. Some more info on that in another question.

Update 2 I guess it is also worth reminding, that when you use some value as a key in dictionary, it should be immutable.

解决方案

I have implemented a tuple library in C#. Visit http://www.adventuresinsoftware.com/generics/ and click on the "tuples" link.

这篇关于在C#中使用一对(三等等)值作为一个值的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆