Created
December 20, 2019 20:14
-
-
Save georgepaoli/699faa32dbe7c8ef4a7012a58e680461 to your computer and use it in GitHub Desktop.
Safe way for get using Dictonary
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
public class DictionaryProxy<TKey, TValue>: Dictionary<TKey, TValue> | |
{ | |
private Dictionary<TKey, TValue> _data; | |
public DictionaryProxy(Dictionary<TKey, TValue> data = null) | |
{ | |
_data = data ?? new Dictionary<TKey, TValue>(); | |
} | |
public TValue this[TKey key] | |
{ | |
get | |
{ | |
if (_data != null && _data.ContainsKey(key)) | |
return _data[key]; | |
return default(TValue); | |
} | |
set | |
{ | |
_data[key] = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment