C#字典,每个键两个值? [英] C# Dictionary with two Values per Key?
问题描述
我在代码的情况下一个词典<字符串,字符串>
似乎是最好的主意 - 我需要这些对象的集合,我需要他们访问通过一个唯一的密钥。正是字典的概念是吧?
I have a situation in code where a Dictionary<string, string>
seemed like the best idea - I need a collection of these objects and I need them to be accessible via a unique key. Exactly what the Dictionary concept is for, right?
那么,要求已扩大到现在我需要持有的每个关键信息的附加位点(一布尔值,如果你很好奇)。
Well, the requirements have expanded to the point where I now need to hold an additional bit of information per-key (a boolean value, if you're curious).
所以,我想将这个概念扩展到创建字符串和布尔一个新的数据结构,并将它现在是一个词典<字符串,NewCustomObject方式>
So, I figure expand the concept to create a new data structure with the string and the boolean and have it now be a Dictionary<string, NewCustomObject>
.
但是,只是一个像一个布尔标志的附加价值,它只是感觉有点小题大做。可是我不知道任何字典类的通用对象的每个关键的两个值。
However, for just one additional value like a boolean flag, it just feels like overkill. And yet I don't know of any Dictionary-like generic object with two values per key.
时只是有定制的字典对象去了解这一点的最好办法或者是有这种情况更简单的东西?
Is just having a Dictionary of custom objects the best way to go about this or is there something simpler for this scenario?
推荐答案
其实,你刚才所描述的是在解释收集的理想使用。它应该包含键:值对,无论其价值的类型。通过使价值自己的类,你就可以在未来轻松地扩展它,在需要时。
Actually, what you've just described is an ideal use for the Dictionary collection. It's supposed to contain key:value pairs, regardless of the type of value. By making the value its own class, you'll be able to extend it easily in the future, should the need arise.
这篇关于C#字典,每个键两个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!