Skip to content

Instantly share code, notes, and snippets.

@margusmartsepp
Last active June 24, 2018 00:19
Show Gist options
  • Save margusmartsepp/d9bce523a19d0475017f to your computer and use it in GitHub Desktop.
Save margusmartsepp/d9bce523a19d0475017f to your computer and use it in GitHub Desktop.
C# Dictionary push and pull
public static class Extensions
{
public static bool Push<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
try
{
if (dictionary.ContainsKey(key))
{
dictionary[key] = value;
}
else
{
dictionary.Add(key, value);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public static TValue Pull<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
{
try
{
return dictionary.ContainsKey(key) ? dictionary[key] : default(TValue);
}
catch (Exception ex)
{
return default(TValue);
}
}
}
@azhe403
Copy link

azhe403 commented Jun 24, 2018

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment