WPF_常用字典扩展方法
来源:互联网 发布:电力监控软件 编辑:程序博客网 时间:2024/04/29 21:28
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; } }
0 0
- WPF_常用字典扩展方法
- WPF_常用集合扩展方法
- OC--字典常用方法
- OC - 字典常用方法
- swift 字典常用方法
- Linq常用扩展方法
- 常用扩展方法总结
- [转]Python-字典常用方法
- 常用的Python字典方法
- Swift 字典的常用方法
- python字典的常用方法
- NSDictionary字典的常用方法
- python的字典常用方法
- STL字符串常用方法扩展
- 扩展easyui常用的方法
- 字典的常用方法(含可变字典)
- python中字典的常用方法
- OC中的字典以及常用方法
- C++中声明与定义的区别
- 孤儿进程
- 稀疏矩阵(sparse matrix)
- 第六章 堆排序
- Cinder meeting 2015-08-22
- WPF_常用字典扩展方法
- Simple Game
- web开发的编码本质
- ListView没有分割线
- POJ 3687 Labeling Balls (逆向建边)
- hdu5384
- No enclosing instance of type WallpaperService is available due to some intermediate constructor inv
- 黑马程序员——java复习总结——泛型和Map
- 用CSS实现层的垂直居中