Increment a numeric value in a Dictionary in C#
This post will discuss how to increment a numeric value in a Dictionary<TKey,TValue> in C#.
1. Using IDictionary<TKey,TValue>.TryGetValue() Method
The IDictionary<TKey,TValue>.TryGetValue() method retrieves the value associated with the specified key if the key is found; otherwise, it sets the value to the default value of the corresponding type of the value parameter. The following code creates an extension method to increment the numeric value of the specified key using the TryGetValue() method.
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static void Increment<T>(this IDictionary<T, int> dict, T key) { int count; dict.TryGetValue(key, out count); dict[key] = count + 1; } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>(); dict["A"] = 1; dict["B"] = 2; dict["C"] = 3; // increment value of key "A" by 1 if exists, otherwise insert it string key = "A"; dict.Increment(key); Console.WriteLine(String.Join(", ", dict)); } } |
Output:
[A, 2], [B, 2], [C, 3]
Beginning from C# 7.0, this can be further shortened using out parameter modifier. It allows us to declare the out variable in the argument list of the method call and prevents a separate variable declaration.
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static void Increment<T>(this IDictionary<T, int> dict, T key) { dict.TryGetValue(key, out var count); dict[key] = count + 1; } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>(); dict["A"] = 1; dict["B"] = 2; dict["C"] = 3; // increment value of key "A" by 1 if exists, otherwise insert it string key = "A"; dict.Increment(key); Console.WriteLine(String.Join(", ", dict)); } } |
Output:
[A, 2], [B, 2], [C, 3]
2. Using Dictionary<TKey,TValue>.ContainsKey() Method
As evident from the above example, TryGetValue() method effectively combines the functionality of the ContainsKey() method and indexer property. The following code example uses the Item[] property increments the existing value of a key if it exists, otherwise, insert it into the dictionary.
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static void Increment<T>(this IDictionary<T, int> dict, T key) { if (!dict.ContainsKey(key)) { dict[key] = 0; } dict[key]++; } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>(); dict["A"] = 1; dict["B"] = 2; dict["C"] = 3; // increment value of key "A" by 1 if exists, otherwise insert it string key = "A"; dict.Increment(key); Console.WriteLine(String.Join(", ", dict)); } } |
Output:
[A, 2], [B, 2], [C, 3]
That’s all about incrementing a numeric value in a Dictionary in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)