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
I would suggest that you create a small custom class exposing the bool and int properties, and override its GetHashCode and Equals methods, then use this as the key.
Could you use a Dictionary<KeyValuePair<int,bool>,int>
?
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.
I think the easiest way to do it now is to use Tupple.Create and ValueTuple.Create:
> var k1 = Tuple.Create("test", int.MinValue, DateTime.MinValue, double.MinValue);
> var k2 = Tuple.Create("test", int.MinValue, DateTime.MinValue, double.MinValue);
> var dict = new Dictionary<object, object>();
> dict.Add(k1, "item");
> dict.Add(k2, "item");
An item with the same key has already been added....
> dict[k1] == dict[k2]
true
or use new c#7 tuple syntax to create tuple-keys:
var k = (item1: "value1", item2: 123);