在C#多维重点哈希表 [英] Hashtable with MultiDimensional Key in C#
本文介绍了在C#多维重点哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我基本上寻找一种方式使用C#中的二维输入的密钥来访问一个哈希表值。
最后,我将能够做这样的事
哈希表[1] [虚假] = 5;
int类型的哈希表= [1] [虚假]
// 1 = 5
这是我一直在努力......没有奏效
Hashtable的试验=新的Hashtable();
test.Add(新字典< INT,BOOL>(){{1,真实}},555);
字典< INT,BOOL>临时=新词典< INT,BOOL>(){{1,真实}};
字符串testz =试验[临时]的ToString();
解决方案
我认为更好的办法是封装的多维关键的许多领域成为一个类/结构。例如:
结构键{
公共只读INT DIMENSION1;
公共只读布尔DIMENSION2;
公钥(INT P1,P2布尔){
DIMENSION1 = P1;
DIMENSION2 = P2;
}
// equals和GetHash code ommitted
}
现在,您可以创建并使用一个正常的哈希表,并使用此包装作为一个密钥。
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
}
Now you can create and use a normal HashTable and use this wrapper as a Key.
这篇关于在C#多维重点哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文