Click here to Skip to main content
Click here to Skip to main content

MultiKeyDictionary generic class.

, 5 Jul 2011
Rate this:
Please Sign up or sign in to vote.

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)

About the Author

Oleg Subachev

Russian Federation Russian Federation
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 5 Jul 2011
Article Copyright 2011 by Oleg Subachev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid