Click here to Skip to main content
15,890,557 members
Articles / Programming Languages / C# 4.0
Tip/Trick

MultiKeyDictionary generic class.

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
5 Jul 2011CPOL 15.2K   4   2

Introduction


Dictionary<TKey, TValue> class allows only one object to serve as a key for its key/value element. But there are situations that require the key to consist of more than one entity.

The MultiKeyDictionary<TKey1, TKey2, TValue> class implements Dictionary<TKey, TValue>-like functionality, but unlike Dictionary<TKey, TValue> each key consists of two parts.


Background


MultiKeyDictionary<TKey1, TKey2, TValue> class is one of several classes implemented in my POSOlSoft.MultiKeyDictionary free .NET component.

Using the Code


Members of the MultiKeyDictionary<TKey1, TKey2, TValue> class are described in the XML documentation contained in the source code.

Usage of the MultiKeyDictionary<TKey1, TKey2, TValue> class is very similar to the Dictionary<TKey, TValue> class usage and is quite straightforward:


MultiKeyDictionary<string, string, int> mkd = new MultiKeyDictionary<string, string, int>();
mkd.Add( "one", "two", 3 );
mkd[ "four", "five" ] = 6;
Console.WriteLine( mkd.Count );
Console.WriteLine( mkd[ "one", "two" ] );
foreach ( var e in mkd )
{
  Console.WriteLine( e );
}
etc...

Points of Interest


Functionality implemented by the MultiKeyDictionary<TKey1, TKey2, TValue> class is similar to functionality of the Dictionary<Tuple<TKey1, TKey2>, TValue> class. But MultiKeyDictionary<TKey1, TKey2, TValue> class is much faster.


Performance comparison of MultiKeyDictionary<TKey1, TKey2, TValue> class against Dictionary<Tuple<TKey1, TKey2>, TValue> class may be found here: http://www.posolsoft.com/products/MKDTupleDictionaryTest.htm.


History


4 July 2011 - first version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder POSOlSoft
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 Plagiarized. Pin
Aron Weiler21-Feb-12 8:16
Aron Weiler21-Feb-12 8:16 
GeneralReason for my vote of 5 Interesting tip! There is actually t... Pin
DrABELL4-Jul-11 10:16
DrABELL4-Jul-11 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.