小编典典

C#中具有多维键的哈希表

c#

我基本上是在寻找一种使用c#中的二维类型键访问哈希表值的方法。

最终我可以做这样的事情

HashTable[1][false] = 5;
int a = HashTable[1][false];
//a = 5

这就是我一直在尝试的…没有用

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();

阅读 435

收藏
2020-05-19

共1个答案

小编典典

我认为更好的方法是将多维键的许多字段封装到类/结构中。例如

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并将此包装用作键。

2020-05-19