Skip to content

Instantly share code, notes, and snippets.

@DouglasLivingstone
Created September 21, 2018 20:35
Show Gist options
  • Save DouglasLivingstone/8e629b12809ce5b7b081c3792755629a to your computer and use it in GitHub Desktop.
Save DouglasLivingstone/8e629b12809ce5b7b081c3792755629a to your computer and use it in GitHub Desktop.
DefaultDictionary for C#
class DefaultDictionary<Key, Value> : Dictionary<Key, Value> {
public new Value this[Key key]
{
get
{
if (!ContainsKey(key))
{
base[key] = default(Value);
}
return base[key];
}
set
{
base[key] = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment