-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryExtensions.cs
More file actions
33 lines (28 loc) · 1.1 KB
/
DictionaryExtensions.cs
File metadata and controls
33 lines (28 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
namespace Ed.Extensions.Dictionary{
public static class DictionaryExtensions {
public static Dictionary<TKey,TValue> Merge<TKey,TValue>(this Dictionary<TKey,TValue> first , Dictionary<TKey,TValue> second,bool overRide ){
foreach(var kvp in second){
if(overRide || !first.ContainsKey(kvp.Key))
first[kvp.Key] = kvp.Value;
}
return first;
}
public static T LookupOrDefault<TKey,T> (this Dictionary<TKey,T> dictionary,TKey key, Func<T> defaultValue ){
T result;
if(!dictionary.TryGetValue(key,out result)){
result = defaultValue();
}
return result;
}
public static T LookupOrDefault<TKey,T> (this Dictionary<TKey,T> dictionary,TKey key ) where T: class {
T result;
if(!dictionary.TryGetValue(key,out result)){
result = Activator.CreateInstance(typeof(T)) as T;
}
return result;
}
}
}