Skip to content

Instantly share code, notes, and snippets.

@georgepaoli
Created December 20, 2019 20:14
Show Gist options
  • Save georgepaoli/699faa32dbe7c8ef4a7012a58e680461 to your computer and use it in GitHub Desktop.
Save georgepaoli/699faa32dbe7c8ef4a7012a58e680461 to your computer and use it in GitHub Desktop.
Safe way for get using Dictonary
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