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; } }
相关文章推荐
- 字符的排序(冒泡排序,选择排序)
- HDU 1285:确定比赛名次 【拓扑排序】
- 2015 多校联赛 ——HDU5389(dp)
- tomcat 8 catalina.out 中文乱码问题解决
- 孤儿进程
- 注解 @Resource
- 2015 多校联赛 ——HDU5389(dp)
- 程序小工具类方面的url收藏
- jquery disable按钮(启用、禁用按钮)
- C++中声明与定义的区别
- 如何组织召开项目的评审会议
- hdu 1711 Number Sequence(kmp找子串第一次出现的位置)
- C#中加粗label的字体
- Emacs 使用graphviz-dot-mode创建架构图
- Elections
- 高级组件之进度条
- 欢迎使用CSDN-markdown编辑器
- JavaScript实现给按钮加上双重动作的方法
- Leetcode 208 Implement Trie (Prefix Tree) 实现字典树 (前缀字典树)
- vs2010 程序语句有错,也报错了 却运行了以前的版本,怎么设置它有错就不要运行了呢?