-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionaryExtensions.cs
60 lines (44 loc) · 1.52 KB
/
DictionaryExtensions.cs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
namespace YamatoDaiwa.CSharpExtensions;
public static class Dictionary
{
/* ━━━ SetPairIfValueNotIsNull ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
public static Dictionary<TKey, TValue> SetPairIfValueNotIsNull<TKey, TValue>(
this Dictionary<TKey, TValue> self,
TKey key,
TValue? value
) where TKey : notnull
{
if (value is not null)
{
self[key] = value;
}
return self;
}
/* ━━━ SetPairIf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
public static Dictionary<TKey, TValue> SetPairIf<TKey, TValue>(
this Dictionary<TKey, TValue> self,
TKey key,
TValue value,
bool condition
) where TKey : notnull
{
if (condition)
{
self[key] = value;
}
return self;
}
public static Dictionary<TKey, TValue> SetPairIf<TKey, TValue>(
this Dictionary<TKey, TValue> self,
TKey key,
TValue value,
Func<TKey, TValue, bool> condition
) where TKey : notnull
{
if (condition(key, value))
{
self[key] = value;
}
return self;
}
}