Skip to content

Instantly share code, notes, and snippets.

@michaelbramwell
Last active March 9, 2016 01:24
Show Gist options
  • Save michaelbramwell/4c6bea2aeedaa6132f7b to your computer and use it in GitHub Desktop.
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"/>
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