Created
October 14, 2016 15:56
-
-
Save caseywatson/3148cba6970ac673f602853557288e62 to your computer and use it in GitHub Desktop.
ElementAtOrDefault for Dictionaries
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
using System; | |
using System.Collections.Generic; | |
namespace Foo | |
{ | |
public static class DictionaryExtensions | |
{ | |
public static TValue ElementAtOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) | |
{ | |
if (dictionary == null) | |
throw new ArgumentNullException(nameof(dictionary)); | |
return (dictionary.ContainsKey(key) ? dictionary[key] : default(TValue)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment