161

I know there isn't one in the BCL but can anyone point me to a good opensource one?

By Multi I mean 2 keys. ;-)

8

16 Answers 16

76

I've also used tuples as jason in his answer does. However, I suggest you simply define a tuple as a struct:

public struct Tuple<T1, T2> { public readonly T1 Item1; public readonly T2 Item2; public Tuple(T1 item1, T2 item2) { Item1 = item1; Item2 = item2;} } public static class Tuple { // for type-inference goodness. public static Tuple<T1,T2> Create<T1,T2>(T1 item1, T2 item2) { return new Tuple<T1,T2>(item1, item2); } } 

You get immutability, .GetHashcode and .Equals for free, which (while you're waiting for C# 4.0) is nice 'n simple...

One warning however: the default GetHashcode implementation (sometimes) only considers the first field so make sure to make the first field the most discriminating or implement GetHashcode yourself (e.g. using FieldwiseHasher.Hash(this) from ValueUtils), otherwise you'll likely run into scalability issues.

Also, you get to avoid nulls which tend to complicate matters (and if you really want nulls, you just make your Tuple<> nullable). Slightly offtopic, am I the only one annoyed at the framework-level lack of support for non-null references? I work on large project, and occasionally a null creeps in somewhere it really shouldn't -- and hey presto, you get a nullreference exception -- but with a stack trace that points you to the reference's first usage, not the actually faulty code.

Of course, .NET 4.0 is pretty old by now; most of us can just use .NET 4.0's tuple.

Edit: to workaround the poor GetHashCode implementation that .NET provides for structs I've written ValueUtils, which also allows you to use real names for your multi-field keys; that means you might write something like:

sealed class MyValueObject : ValueObject<MyValueObject> { public DayOfWeek day; public string NamedPart; //properties work fine too } 

...which hopefully makes it easier to have human-readable names for data with value semantics, at least until some future version of C# implements proper tuples with named members; hopefully with decent hashcodes ;-).

Sign up to request clarification or add additional context in comments.

18 Comments

Nice blurb on reference versus value-type decision for tuples in .NET: msdn.microsoft.com/en-us/magazine/dd942829.aspx#id0400060
@nawfal: I decided to reimplement the hashcode function generator. It works; but it's still pretty barebones: github.com/EamonNerbonne/ValueUtils
@EamonNerbonne looks good. I'm not very sure about OverriddenHashCodeMethod's job. Will have to test.
@nawfal: I'll polish it up and add more features, and release it to nuget. If you actually intend to use this, I'd love feedback on how to make it work for your usecase - feel free to comment on github!
@nawfal: OK, I think it's in a pretty usable state now. I'd be interested in your opinion on two specific design issues: github.com/EamonNerbonne/ValueUtils/issues/1 and github.com/EamonNerbonne/ValueUtils/issues/2. Even so, this is ready for real use - just try refering to nuget:ValueUtils, and create your class ala sealed class MyClass : ValueObject<MyClass>! Early perf testing shows it to be much faster than ValueType, considerably faster than Tuple<>, and within a factor 2 of hand-rolled code.
|
61

I use a Tuple as the keys in a Dictionary.

public class Tuple<T1, T2> { public T1 Item1 { get; private set; } public T2 Item2 { get; private set; } // implementation details } 

Be sure to override Equals and GetHashCode and define operator!= and operator== as appropriate. You can expand the Tuple to hold more items as needed. .NET 4.0 will include a built-in Tuple.

3 Comments

Check out this blog post on performance tests between multiple "multi-key" dictionary implementations here
@AronW The blog post never uses a Tuple as a key as far as I can tell?
If you're going to write your own Tuple class, may as well use named items instead of T1, T2...
39

Tuples will be (are) in .Net 4.0 Until then, you can also use a

 Dictionary<key1, Dictionary<key2, TypeObject>> 

or, creating a custom collection class to represent this...

 public class TwoKeyDictionary<K1, K2, T>: Dictionary<K1, Dictionary<K2, T>> { } 

or, with three keys...

public class ThreeKeyDictionary<K1, K2, K3, T> : Dictionary<K1, Dictionary<K2, Dictionary<K3, T>>> { } 

3 Comments

Brilliant answer -- this worked perfectly for my needs.
This is not multi-key dictionary, this is quite bad implementation of a tupled-key dictionary, because you can't access values by second key, neither you can access them without it (unless you get it by Values of inner dictionary, but it can contain multiple items). Also, your solution lacks code for adding and removing items.
jeeez... I'm not even sure what you are talking about. You want a multi-keyed dictionary to be able to access objects with either of the keys? That is in no way equivalent to a tuple as requested by the op. To your second, you really expect a fully fleshed out professional implementation, with all the bells and whistles, as an answer to the question? Dream on, I get paid for that. Write your own code.
26

Many good solutions here, What I am missing here is an implementation based on the build in Tuple type, so I wrote one myself.

Since it just inherits from Dictionary<Tuple<T1,T2>, T> you can always use both ways.

var dict = new Dictionary<int, int, Row>(); var row = new Row(); dict.Add(1, 2, row); dict.Add(Tuple.Create(1, 2, row)); dict.Add(new Tuple<int, int>(1, 2)); 

here is the code.

public class Dictionary<TKey1,TKey2,TValue> : Dictionary<Tuple<TKey1, TKey2>, TValue>, IDictionary<Tuple<TKey1, TKey2>, TValue> { public TValue this[TKey1 key1, TKey2 key2] { get { return base[Tuple.Create(key1, key2)]; } set { base[Tuple.Create(key1, key2)] = value; } } public void Add(TKey1 key1, TKey2 key2, TValue value) { base.Add(Tuple.Create(key1, key2), value); } public bool ContainsKey(TKey1 key1, TKey2 key2) { return base.ContainsKey(Tuple.Create(key1, key2)); } } 

Please be aware that this implementation depends on the Tuple.Equals() implementation itself:

http://msdn.microsoft.com/en-us/library/dd270346(v=vs.110).aspx

The obj parameter is considered to be equal to the current instance under the following conditions:

  • It is a Tuple object.
  • Its two components are of the same types as the current instance.
  • Its two components are equal to those of the current instance. Equality is determined by the default object equality comparer for each component.

1 Comment

Very nice solution
15

I wrote and have used this with success.

public class MultiKeyDictionary<K1, K2, V> : Dictionary<K1, Dictionary<K2, V>> { public V this[K1 key1, K2 key2] { get { if (!ContainsKey(key1) || !this[key1].ContainsKey(key2)) throw new ArgumentOutOfRangeException(); return base[key1][key2]; } set { if (!ContainsKey(key1)) this[key1] = new Dictionary<K2, V>(); this[key1][key2] = value; } } public void Add(K1 key1, K2 key2, V value) { if (!ContainsKey(key1)) this[key1] = new Dictionary<K2, V>(); this[key1][key2] = value; } public bool ContainsKey(K1 key1, K2 key2) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2); } public new IEnumerable<V> Values { get { return from baseDict in base.Values from baseKey in baseDict.Keys select baseDict[baseKey]; } } } public class MultiKeyDictionary<K1, K2, K3, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, V>> { public V this[K1 key1, K2 key2, K3 key3] { get { return ContainsKey(key1) ? this[key1][key2, key3] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, V>(); this[key1][key2, key3] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3); } } public class MultiKeyDictionary<K1, K2, K3, K4, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, V>(); this[key1][key2, key3, key4] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, V>(); this[key1][key2, key3, key4, key5] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, V>(); this[key1][key2, key3, key4, key5, key6] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, V>(); this[key1][key2, key3, key4, key5, key6, key7] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, V>(); this[key1][key2, key3, key4, key5, key6, key7, key8] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, V>(); this[key1][key2, key3, key4, key5, key6, key7, key8, key9] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, V>(); this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10); } } public class MultiKeyDictionary<K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V> : Dictionary<K1, MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V>> { public V this[K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10, K11 key11] { get { return ContainsKey(key1) ? this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10, key11] : default(V); } set { if (!ContainsKey(key1)) this[key1] = new MultiKeyDictionary<K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, V>(); this[key1][key2, key3, key4, key5, key6, key7, key8, key9, key10, key11] = value; } } public bool ContainsKey(K1 key1, K2 key2, K3 key3, K4 key4, K5 key5, K6 key6, K7 key7, K8 key8, K9 key9, K10 key10, K11 key11) { return base.ContainsKey(key1) && this[key1].ContainsKey(key2, key3, key4, key5, key6, key7, key8, key9, key10, key11); } } 

4 Comments

I wrote a ToMultiKeyDictionary() extension for this answer and posted it as an answer below
This works great in Unity where Tuple is not available!!! Note that u need using System.Linq;
Works well when both values put together function as a unique key.
I am using 4 keys and a value. How to get all values without passing any key?
9

I frequently use this because it's short and provides the syntactic sugar I need...

public class MultiKeyDictionary<T1, T2, T3> : Dictionary<T1, Dictionary<T2, T3>> { new public Dictionary<T2, T3> this[T1 key] { get { if (!ContainsKey(key)) Add(key, new Dictionary<T2, T3>()); Dictionary<T2, T3> returnObj; TryGetValue(key, out returnObj); return returnObj; } } } 

To use it:

dict[cat][fish] = 9000; 

where the "Cat" key doesn't have to exist either.

4 Comments

I should also mention that it's arbitrary to create further nestings with another class <T1, T2, T3, T4> : Dictionary<T1, MultiKeyDictionary<T2, T3>>, etc. Also, I avoid using it, but it cleans up funky nested dictionaries pretty well.
What's a good way to check if the [cat][fish] or [cat][mouse] keys exist?
@goku_da_master dict[cat].ContainsKey(mouse) where if cat doesn't exist, you always get false, because it is newed up then. If you're using it this way a lot I would benchmark it though--it's not going to be a very optimized way of doing this.
Underrated solution! (: Very handy when using .NET earlier than 4.0.
7

I'm currently simply concatenating the keys into a single string as a workaround. Of course, this will not work on non-string keys. Would love to know the answer as well.

8 Comments

Because on most class .ToSting() will be the same for all values (ie. the types name). Not all types have a valid string representation, etc, etc.
Then, one cannot use sealed classes as keys, and instead of implementing one class which is a tuple of many, one must subclass many classes.
Yeah, too many BCL classes don't implement ToString, which means you can't assume ToString will return a unique representation of a given type's state. TL;DR: fail
Using string concatenation can be dangerous. For example, if you have ints 123 and 456, and concatenate them to create a key, this would give "123456". If you have 12 and 3456 and you concatenate them, you get... "123456". Oops.
@Meta-Knight Use separator. Duh.
|
6

Take a look at Wintellect's PowerCollections (CodePlex download). I think their MultiDictionary does something like that.

It's a dictionary of dictionaries, so you have 2 keys to access each object, the key for the main dictionary to get you the required sub dictionary, and then the second key for the sub dictionary to get you the required item. Is that what you mean?

3 Comments

Their MultiDictionary<,> allows to assign multiple values to a single key, not to have multiple keys in this sense.
but OP's question is as vague as it can get. Hence +1 for a different take.
@pbalaga is right, PowerCollections doesn't allow you to have two keys (downvoted because of that)
5

I've googled for this one: http://www.codeproject.com/KB/recipes/multikey-dictionary.aspx. I guess it's main feature compared to using struct to contain 2 keys in regular dictionary is that you can later reference by one of the keys, instead of having to supply 2 keys.

Comments

5

Is there anything wrong with

new Dictionary<KeyValuePair<object, object>, object>
?

3 Comments

Is there a built-in Pair<T1, T2> class?
@MichaelDonohue: As of .NET 4.0, there is the Tuple class.
Yes, there is something wrong: KVP uses ValueType.GetHashCode - "If you call the derived type's GetHashCode method, the return value is not likely to be suitable for use as a key in a hash table." KVP is a completely inappropriate choice for a Dictionary key and could yield many collisions.
4

Here's a fleshed out example of a pair class which can be used as the key to a Dictionary.

public class Pair<T1, T2> { public T1 Left { get; private set; } public T2 Right { get; private set; } public Pair(T1 t1, T2 t2) { Left = t1; Right = t2; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != typeof(Pair<T1, T2>)) return false; return Equals((Pair<T1, T2>)obj); } public bool Equals(Pair<T1, T2> obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; return Equals(obj.Left, Left) && Equals(obj.Right, Right); } public override int GetHashCode() { unchecked { return (Left.GetHashCode() * 397) ^ Right.GetHashCode(); } } } 

Comments

3

Could you use a Dictionary<TKey1,Dictionary<TKey2,TValue>>?

You could even subclass this:

public class DualKeyDictionary<TKey1,TKey2,TValue> : Dictionary<TKey1,Dictionary<TKey2,TValue>> 

EDIT: This is now a duplicate answer. It also is limited in its practicality. While it does "work" and provide ability to code dict[key1][key2], there are lots of "workarounds" to get it to "just work".

HOWEVER: Just for kicks, one could implement Dictionary nonetheless, but at this point it gets a little verbose:

public class DualKeyDictionary<TKey1, TKey2, TValue> : Dictionary<TKey1, Dictionary<TKey2, TValue>> , IDictionary< object[], TValue > { #region IDictionary<object[],TValue> Members void IDictionary<object[], TValue>.Add( object[] key, TValue value ) { if ( key == null || key.Length != 2 ) throw new ArgumentException( "Invalid Key" ); TKey1 key1 = key[0] as TKey1; TKey2 key2 = key[1] as TKey2; if ( !ContainsKey( key1 ) ) Add( key1, new Dictionary<TKey2, TValue>() ); this[key1][key2] = value; } bool IDictionary<object[], TValue>.ContainsKey( object[] key ) { if ( key == null || key.Length != 2 ) throw new ArgumentException( "Invalid Key" ); TKey1 key1 = key[0] as TKey1; TKey2 key2 = key[1] as TKey2; if ( !ContainsKey( key1 ) ) return false; if ( !this[key1].ContainsKey( key2 ) ) return false; return true; } 

2 Comments

I see, Charles is thinking the same thing. The problem with these is, however, in the allocation. It might be a beast to manage.
+1 for the naming, DualKey.. Have been looking for a good name :)
3

If anyone is looking for a ToMultiKeyDictionary() here is an implementation that should work with most of the answers here (based on Herman's):

public static class Extensions_MultiKeyDictionary { public static MultiKeyDictionary<K1, K2, V> ToMultiKeyDictionary<S, K1, K2, V>(this IEnumerable<S> items, Func<S, K1> key1, Func<S, K2> key2, Func<S, V> value) { var dict = new MultiKeyDictionary<K1, K2, V>(); foreach (S i in items) { dict.Add(key1(i), key2(i), value(i)); } return dict; } public static MultiKeyDictionary<K1, K2, K3, V> ToMultiKeyDictionary<S, K1, K2, K3, V>(this IEnumerable<S> items, Func<S, K1> key1, Func<S, K2> key2, Func<S, K3> key3, Func<S, V> value) { var dict = new MultiKeyDictionary<K1, K2, K3, V>(); foreach (S i in items) { dict.Add(key1(i), key2(i), key3(i), value(i)); } return dict; } } 

Comments

2

I think you would need a Tuple2 like class. Be sure that it's GetHashCode() and Equals() is based upon the two contained elements.

See Tuples in C#

Comments

2

Here's my implementation. I wanted something to hide the implementation of the Tuple concept.

 public class TwoKeyDictionary<TKey1, TKey2, TValue> : Dictionary<TwoKey<TKey1, TKey2>, TValue> { public static TwoKey<TKey1, TKey2> Key(TKey1 key1, TKey2 key2) { return new TwoKey<TKey1, TKey2>(key1, key2); } public TValue this[TKey1 key1, TKey2 key2] { get { return this[Key(key1, key2)]; } set { this[Key(key1, key2)] = value; } } public void Add(TKey1 key1, TKey2 key2, TValue value) { Add(Key(key1, key2), value); } public bool ContainsKey(TKey1 key1, TKey2 key2) { return ContainsKey(Key(key1, key2)); } } public class TwoKey<TKey1, TKey2> : Tuple<TKey1, TKey2> { public TwoKey(TKey1 item1, TKey2 item2) : base(item1, item2) { } public override string ToString() { return string.Format("({0},{1})", Item1, Item2); } } 

It helps keeps the usage looking like a Dictionary

item.Add(1, "D", 5.6); value = item[1, "D"]; 

1 Comment

how to iterate through all?
1

Here's another example using the Tuple class with the Dictionary.

 // Setup Dictionary Dictionary<Tuple<string, string>, string> testDictionary = new Dictionary<Tuple<string, string>, string> { {new Tuple<string, string>("key1","key2"), "value1"}, {new Tuple<string, string>("key1","key3"), "value2"}, {new Tuple<string, string>("key2","key3"), "value3"} }; //Query Dictionary public string FindValue(string stuff1, string stuff2) { return testDictionary[Tuple.Create(stuff1, stuff2)]; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.