Last active
March 9, 2016 01:24
-
-
Save michaelbramwell/4c6bea2aeedaa6132f7b to your computer and use it in GitHub Desktop.
Generic get/set operation for types that are stored in an implementation of <see cref="IDictionary"/>
This file contains 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 T GetFromDictionary<T>(string key, IDictionary items, Func<T> whenKeyNotFound) | |
{ | |
if (items[key] != null) | |
{ | |
// return from dictionary | |
return (T)items[key]; | |
} | |
// return new item from caller and add to dictionary | |
var item = whenKeyNotFound(); | |
items.Add(key, item); | |
return item; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment