Skip to content

Instantly share code, notes, and snippets.

@MrModest
Last active May 31, 2018 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrModest/236423a38a853ce92a48b8c97179eaa1 to your computer and use it in GitHub Desktop.
Save MrModest/236423a38a853ce92a48b8c97179eaa1 to your computer and use it in GitHub Desktop.
Implementation for IDictionary with custom default value if key doesn't exist
//Author: John Sonmez | https://stackoverflow.com/users/45365 | https://github.com/jsonmez
//Source: https://stackoverflow.com/a/7061653/7422280
//GitHub: https://github.com/jsonmez/Defaultable-Dictionary
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace DefaultableDictionary
{
public class DefaultableDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> dictionary;
private readonly TValue defaultValue;
#region Constructors
public DefaultableDictionary()
{
this.dictionary = new Dictionary<TKey, TValue>();
this.defaultValue = default(TValue);
}
public DefaultableDictionary(TValue defaultValue)
{
this.dictionary = new Dictionary<TKey, TValue>();
this.defaultValue = defaultValue;
}
public DefaultableDictionary(IDictionary<TKey, TValue> dictionary)
{
this.dictionary = dictionary;
this.defaultValue = default(TValue);
}
public DefaultableDictionary(IDictionary<TKey, TValue> dictionary, TValue defaultValue)
{
this.dictionary = dictionary;
this.defaultValue = defaultValue;
}
#endregion
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<TKey, TValue> item)
{
dictionary.Add(item);
}
public void Clear() {
dictionary.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return dictionary.Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
dictionary.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return dictionary.Remove(item);
}
public int Count
{
get { return dictionary.Count; }
}
public bool IsReadOnly
{
get { return dictionary.IsReadOnly; }
}
public bool ContainsKey(TKey key)
{
return dictionary.ContainsKey(key);
}
public void Add(TKey key, TValue value)
{
dictionary.Add(key, value);
}
public bool Remove(TKey key)
{
return dictionary.Remove(key);
}
public bool TryGetValue(TKey key, out TValue value)
{
if (!dictionary.TryGetValue(key, out value))
{
value = defaultValue;
}
return true;
}
public TValue this[TKey key]
{
get
{
try
{
return dictionary[key];
}
catch (KeyNotFoundException)
{
return defaultValue;
}
}
set { dictionary[key] = value; }
}
public ICollection<TKey> Keys
{
get { return dictionary.Keys; }
}
public ICollection<TValue> Values
{
get
{
var values = new List<TValue>(dictionary.Values)
{
defaultValue
};
return values;
}
}
}
public static class DefaultableDictionaryExtensions
{
public static IDictionary<TKey, TValue> WithDefaultValue<TValue, TKey>(this IDictionary<TKey, TValue> dictionary, TValue defaultValue )
{
return new DefaultableDictionary<TKey, TValue>(dictionary, defaultValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment