using System.Collections;
using System.Collections.Generic;
public static void Main()
var lkp = lst.ToLookup(x=>x.a);
Console.WriteLine("Lookup.");
Console.WriteLine($"1: '{lkp[1].FirstOrDefault()}'");
Console.WriteLine($"2: '{lkp[2].FirstOrDefault()}'");
var dic = lst.ToDictionary(x=>x.a);
var defaultDic = dic.ToDefault();
Console.WriteLine("\r\nDefault Dictionary.");
Console.WriteLine($"1: '{defaultDic[1]}'");
Console.WriteLine($"2: '{defaultDic[2]}'");
Console.WriteLine("\r\nDictionary.");
Console.WriteLine($"1: '{dic[1]}'");
Console.WriteLine($"2: '{dic[2]}'");
public static class DefaultExtesions{
public static DefaultDictionary<K,T> ToDefault<K,T>(this IDictionary<K, T> dictionaryToWrap)
return new DefaultDictionary<K,T>(dictionaryToWrap);
public class DefaultDictionary<K, T> : IDictionary<K, T>
private IDictionary<K, T> _wrappedDictionary;
internal DefaultDictionary(IDictionary<K,T> dictionaryToWrap)
_wrappedDictionary = dictionaryToWrap;
if (!_wrappedDictionary.ContainsKey(index))
return _wrappedDictionary[index];
_wrappedDictionary[index] = value;
public ICollection<K> Keys
return _wrappedDictionary.Keys;
public ICollection<T> Values
return _wrappedDictionary.Values;
return _wrappedDictionary.Count;
return _wrappedDictionary.IsReadOnly;
public void Add(K key, T value)
_wrappedDictionary.Add(key,value);
public void Add(KeyValuePair<K, T> item)
_wrappedDictionary.Add(item);
_wrappedDictionary.Clear();
public bool Contains(KeyValuePair<K, T> item)
return _wrappedDictionary.Contains(item);
public bool ContainsKey(K key)
return _wrappedDictionary.ContainsKey(key);
public void CopyTo(KeyValuePair<K, T>[] array, int arrayIndex)
_wrappedDictionary.CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<K, T>> GetEnumerator()
return _wrappedDictionary.GetEnumerator();
public bool Remove(K key)
return _wrappedDictionary.Remove(key);
public bool Remove(KeyValuePair<K, T> item)
return _wrappedDictionary.Remove(item);
public bool TryGetValue(K key, out T value)
return _wrappedDictionary.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator()
return _wrappedDictionary.GetEnumerator();