Guest User

Untitled

a guest
Feb 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. namespace SnowMaker
  2. {
  3. static class DictionaryExtensions
  4. {
  5. internal static TValue GetValue<TKey, TValue>(
  6. this IDictionary<TKey, TValue> dictionary,
  7. TKey key,
  8. object dictionaryLock,
  9. Func<TValue> valueInitializer)
  10. {
  11. TValue value;
  12. var found = dictionary.TryGetValue(key, out value);
  13. if (found) return value;
  14.  
  15. lock (dictionaryLock)
  16. {
  17. found = dictionary.TryGetValue(key, out value);
  18. if (found) return value;
  19.  
  20. value = valueInitializer();
  21.  
  22. dictionary.Add(key, value);
  23. }
  24.  
  25. return value;
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment