本文介绍了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
这就是我一直在尝试的...没有奏效
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();
推荐答案
我认为更好的方法是将您的多维键的许多字段封装到一个类/结构中.例如
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#中带有多维键的哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!