您的位置:首页 > 其它

WPF_常用字典扩展方法

2015-08-14 11:28 323 查看
public static class DictionaryExtensions
{
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> toAction)
{
foreach (var pair in dictionary)
{
toAction(pair.Key, pair.Value);
}
}

public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue, int> toAction)
{
int i = 0;
foreach (var pair in dictionary)
{
toAction(pair.Key, pair.Value, i++);
}
}

public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : default(TValue);
}

public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defalutValue)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defalutValue;
}

public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> defalutValueFactory)
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defalutValueFactory(key);
}

public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
return dictionary.GetOrAdd(key, x => value);
}

public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> addValueFactory)
{
TValue value;
if (!dictionary.TryGetValue(key, out value))
{
value = addValueFactory(key);
dictionary.Add(key, value);
}

return value;
}

public static void AddOrUpdate<TKey1, TKey2, TValue>(
this Dictionary<TKey1, Dictionary<TKey2, TValue>> dictionary, TKey1 key1, TKey2 key2, TValue value)
{
Dictionary<TKey2, TValue> dictionary2;
if (!dictionary.TryGetValue(key1, out dictionary2))
{
dictionary2 = new Dictionary<TKey2, TValue>();
dictionary.Add(key1, dictionary2);
}
dictionary2[key2] = value;
}

public static TValue AddOrUpdate<TKey, TValue>(
this Dictionary<TKey, TValue> dictionary,
TKey key,
Func<TKey, TValue> addValueFactory,
Func<TKey, TValue, TValue> updateValueFactory)
{
TValue value;
if (dictionary.TryGetValue(key, out value))
{
value = updateValueFactory(key, value);
dictionary[key] = value;
}
else
{
value = addValueFactory(key);
dictionary.Add(key, value);
}

return value;
}

public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> kvps)
{
var collection = (ICollection<KeyValuePair<TKey, TValue>>)dictionary;
kvps.ForEach(kvp => collection.Add(kvp));
}

public static void AddOrUpdateRange<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> kvps)
{
kvps.ForEach(pair => dictionary[pair.Key] = pair.Value);
}

public static bool TryGetValue<TKey1, TKey2, TValue>(
this Dictionary<TKey1, Dictionary<TKey2, TValue>> dictionary, TKey1 key1, TKey2 key2, out TValue value)
{
Dictionary<TKey2, TValue> dictionary2;
if (dictionary.TryGetValue(key1, out dictionary2))
return dictionary2.TryGetValue(key2, out value);

value = default(TValue);
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: