C#中具有多维键的哈希表 [英] Hashtable with MultiDimensional Key in C#
本文介绍了C#中具有多维键的哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基本上正在寻找一种在c#中使用二维类型键访问散列表值的方法。
I'm basically looking for a way to access a hashtable value using a two-dimensional typed key in c#.
最终我可以做一些像这个
Eventually I would be able to do something like this
HashTable[1][false] = 5;
int a = HashTable[1][false];
//a = 5
这是我一直在尝试... hasn没有工作
This is what I've been trying...hasn't worked
Hashtable test = new Hashtable();
test.Add(new Dictionary<int, bool>() { { 1, true } }, 555);
Dictionary<int, bool> temp = new Dictionary<int, bool>() {{1, true}};
string testz = test[temp].ToString();
推荐答案
我认为一个更好的方法是封装许多字段的多维键转换成一个类/ struct。例如
I think a better approach is to encapsulate the many fields of your multi-dimensional key into a class / struct. For example
struct Key {
public readonly int Dimension1;
public readonly bool Dimension2;
public Key(int p1, bool p2) {
Dimension1 = p1;
Dimension2 = p2;
}
// Equals and GetHashCode ommitted
}
现在您可以创建并使用正常的HashTable,并将此包装器用作Key。
Now you can create and use a normal HashTable and use this wrapper as a Key.
这篇关于C#中具有多维键的哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文