Created
September 21, 2018 20:35
-
-
Save DouglasLivingstone/8e629b12809ce5b7b081c3792755629a to your computer and use it in GitHub Desktop.
DefaultDictionary for C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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