[TryAdd]KeyがなければValueと共に追加します。 #ExtensionMethod
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue addValue)
{
bool canAdd = !dict.ContainsKey(key);
if (canAdd)
{
dict.Add(key, addValue);
}
return canAdd;
}
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue> addValueFactory)
{
bool canAdd = !dict.ContainsKey(key);
if (canAdd)
{
dict.Add(key, addValueFactory(key));
}
return canAdd;
}