-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dict.cs
42 lines (40 loc) · 1.23 KB
/
Dict.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Master.Utility
{
/// <summary>
/// Helper class for adding values to map
/// Author: Tomislav
/// </summary>
class Dict
{
/// <summary>
/// Helper method for dealing with adding values to map.
/// </summary>
/// <typeparam name="K">Map key type</typeparam>
/// <typeparam name="V">Map value type</typeparam>
/// <param name="dict">Map</param>
/// <param name="key">Map key</param>
/// <param name="addValue">Value to add</param>
public static void addToDict<K, V>(Dictionary<K, V> dict, K key, V addValue) {
V count = default(V);
dict.TryGetValue(key, out count);
if (count.Equals(default(V))) {
dict.Add(key, addValue);
}
else {
dynamic oldValue = count;
dynamic newValue = addValue;
try {
dict[key] = oldValue + newValue;
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
Environment.Exit(0);
}
}
}
}
}