using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EasyDevCore.Common { /// /// /// public static class DictionaryHelper { /// /// Tries to get a value in dictionary. /// /// The type of the key. /// The type of the value. /// The dictionary. /// The key. /// The default value if key not exists. /// public static TValue GetOrDefault(this IDictionary dict, TKey key, TValue defaultValue) => dict.ContainsKey(key) ? dict[key] : defaultValue; /// /// Gets the or add. /// /// The type of the key. /// The type of the value. /// The dictionary. /// The key. /// The default value will add and return if key not exits /// public static TValue GetOrAdd(this IDictionary dict, TKey key, TValue defaultValue) { if (!dict.ContainsKey(key)) { dict[key] = defaultValue; } return (TValue)dict[key]; } } }