Add values to a Dictionary in C#
This post will discuss how to add values to a Dictionary in C#.
1. Using Dictionary<TKey,TValue>.Add(TKey, TValue) method
The Dictionary.Add() method can be used to add the specified key and value to a dictionary. The following method demonstrates how to use the Add method for adding a new key-value pair to an existing 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 |
using System; using System.Collections.Generic; public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; dict.Add("key3", "value3"); foreach (var (key, value) in dict) { Console.WriteLine(key + " : " + value); } } } /* Output: key1 : value1 key2 : value2 key3 : value3 */ |
The above code throws an ArgumentException when attempting to add a duplicate key. This can be handled using a try-catch block. The following example demonstrates how to handle the exception if a value with the specified key already exists.
|
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; public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; try { dict.Add("key2", "value3"); } catch (ArgumentException) { Console.WriteLine("An item with the same key has already been added."); } foreach (var (key, value) in dict) { Console.WriteLine(key + " : " + value); } } } /* Output: An item with the same key has already been added. key1 : value1 key2 : value2 */ |
2. Using Dictionary<TKey,TValue>.Item[TKey] Property
If you need to update the value for a specific key already present in the Dictionary, you can use the dict[key] = value syntax. This syntax adds the specified key and value to the dictionary if the key does not exist or updates the existing value for the specified key if the key already exists in the dictionary. This is demonstrated below:
|
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 |
using System; using System.Collections.Generic; public static class Extensions { public static void AddOrUpdateKey<T>(this Dictionary<T, T> dict, T key, T newValue) { if (dict.ContainsKey(key)) { dict[key] = newValue; } else { dict.Add(key, newValue); } } } public class Example { public static void Main() { Dictionary<string, string> dict = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; dict.AddOrUpdateKey("key2", "value3"); foreach (var (key, value) in dict) { Console.WriteLine(key + " : " + value); } } } /* Output: key1 : value1 key2 : value3 */ |
That’s all about adding values to 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 :)