Last active
June 24, 2018 00:19
-
-
Save margusmartsepp/d9bce523a19d0475017f to your computer and use it in GitHub Desktop.
C# Dictionary push and pull
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 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks