Advertisement
BurningBunny

MultiValue Dictionary

Dec 25th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. /*
  2.  *
  3.  *  http://csharpcodewhisperer.blogspot.com
  4.  *
  5.  * Made using SharpDevelop
  6.  *
  7.  */
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11.  
  12. namespace MultiValueDictionary
  13. {
  14.     public class MultiValueDictionary<K,V>
  15.     {
  16.         Dictionary<K, List<V>> _dictionary = new Dictionary<K, List<V>>();
  17.  
  18.         public void Add(K key, V value)
  19.         {
  20.             List<V> list;
  21.             if (this._dictionary.TryGetValue(key, out list))
  22.             {
  23.                 list.Add(value);
  24.             }
  25.             else
  26.             {
  27.                 list = new List<V>();
  28.                 list.Add(value);
  29.                 this._dictionary[key] = list;
  30.             }
  31.         }
  32.        
  33.         public int Count
  34.         {
  35.             get { return this._dictionary.Count; }
  36.         }
  37.  
  38.         public IEnumerable<K> Keys
  39.         {
  40.             get
  41.             {
  42.                 return this._dictionary.Keys;
  43.             }
  44.         }
  45.        
  46.         public IEnumerable<List<V>> Values
  47.         {
  48.             get {   return this._dictionary.Values; }
  49.         }
  50.  
  51.         public List<V> this[K key]
  52.         {
  53.             get
  54.             {
  55.                 List<V> list;
  56.                 if (!this._dictionary.TryGetValue(key, out list))
  57.                 {
  58.                     list = new List<V>();
  59.                     this._dictionary[key] = list;
  60.                 }
  61.                 return list;
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement