Sco*_*ess 62 c# dictionary hashtable
我基本上是在寻找一种方法来使用c#中的二维类型键来访问哈希表值.
最终我可以做这样的事情
HashTable[1][false] = 5;
int a = HashTable[1][false];
//a = 5
Run Code Online (Sandbox Code Playgroud)
这就是我一直在尝试的......没有用
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();
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 68
我认为更好的方法是将多维键的许多字段封装到类/结构中.例如
struct Key {
public readonly int Dimension1;
public readonly bool Dimension2;
public Key(int p1, bool p2) {
Dimension1 = p1;
Dimension2 = p2;
}
// Equals and GetHashCode ommitted
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以创建和使用普通的HashTable,并将此包装器用作Key.
小智 23
如何使用具有某种元组结构的常规字典作为键?
public class TwoKeyDictionary<K1,K2,V>
{
private readonly Dictionary<Pair<K1,K2>, V> _dict;
public V this[K1 k1, K2 k2]
{
get { return _dict[new Pair(k1,k2)]; }
}
private struct Pair
{
public K1 First;
public K2 Second;
public override Int32 GetHashCode()
{
return First.GetHashCode() ^ Second.GetHashCode();
}
// ... Equals, ctor, etc...
}
}
Run Code Online (Sandbox Code Playgroud)
Hüs*_*ğlı 21
您现在可以使用新元组在C#7.0中执行此操作:
// Declare
var test = new Dictionary<(int, bool), int>();
// Add
test.Add((1, false), 5);
// Get
int a = test[(1, false)];
Run Code Online (Sandbox Code Playgroud)
Par*_*Joe 19
以防万一有人最近在这里,如一个评论者所描述的,如何在.Net 4.0中快速而肮脏的方式这样做的例子.
class Program
{
static void Main(string[] args)
{
var twoDic = new Dictionary<Tuple<int, bool>, String>();
twoDic.Add(new Tuple<int, bool>(3, true), "3 and true." );
twoDic.Add(new Tuple<int, bool>(4, true), "4 and true." );
twoDic.Add(new Tuple<int, bool>(3, false), "3 and false.");
// Will throw exception. Item with the same key already exists.
// twoDic.Add(new Tuple<int, bool>(3, true), "3 and true." );
Console.WriteLine(twoDic[new Tuple<int, bool>(3,false)]);
Console.WriteLine(twoDic[new Tuple<int, bool>(4,true)]);
// Outputs "3 and false." and "4 and true."
}
}
Run Code Online (Sandbox Code Playgroud)
Jas*_*yon 16
我想这可能更接近你正在寻找的东西......
var data = new Dictionary<int, Dictionary<bool, int>>();
Run Code Online (Sandbox Code Playgroud)
Dictonary您需要一个正确实现的关键类GetHashCode。并且您可以扩展Dictonary以让您以友好的方式访问它。
班上KeyPair:
public class KeyPair<Tkey1, Tkey2>
{
public KeyPair(Tkey1 key1, Tkey2 key2)
{
Key1 = key1;
Key2 = key2;
}
public Tkey1 Key1 { get; set; }
public Tkey2 Key2 { get; set; }
public override int GetHashCode()
{
return Key1.GetHashCode() ^ Key2.GetHashCode();
}
public override bool Equals(object obj)
{
KeyPair<Tkey1, Tkey2> o = obj as KeyPair<Tkey1, Tkey2>;
if (o == null)
return false;
else
return Key1.Equals(o.Key1) && Key2.Equals(o.Key2);
}
}
Run Code Online (Sandbox Code Playgroud)
延长Dictonary<>:
public class KeyPairDictonary<Tkey1, Tkey2, Tvalue>
: Dictionary<KeyPair<Tkey1, Tkey2>, Tvalue>
{
public Tvalue this[Tkey1 key1, Tkey2 key2]
{
get
{
return this[new KeyPair<Tkey1, Tkey2>(key1, key2)];
}
set
{
this[new KeyPair<Tkey1, Tkey2>(key1, key2)] = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样使用它:
KeyPairDictonary<int, bool, string> dict =
new KeyPairDictonary<int, bool, string>();
dict[1, false] = "test";
string test = dict[1, false];
Run Code Online (Sandbox Code Playgroud)
小智 5
我建议jachymko的解决方案略有不同,这将允许您避免为密钥对创建一个类.而是包装一个字典的私人字典,如下所示:
public class MultiDictionary<K1, K2, V>
{
private Dictionary<K1, Dictionary<K2, V>> dict =
new Dictionary<K1, Dictionary<K2, V>>();
public V this[K1 key1, K2 key2]
{
get
{
return dict[key1][key2];
}
set
{
if (!dict.ContainsKey(key1))
{
dict[key1] = new Dictionary<K2, V>();
}
dict[key1][key2] = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72653 次 |
| 最近记录: |