Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Add key-value pair in C# Dictionary
To add key-value pairs in C# Dictionary, you can use several methods. The Dictionary class provides multiple ways to insert elements, each with its own advantages for different scenarios.
Syntax
Following are the main syntaxes for adding key-value pairs to a Dictionary −
// Using Add() method with separate key and value dictionary.Add(key, value); // Using Add() method with KeyValuePair dictionary.Add(new KeyValuePair<TKey, TValue>(key, value)); // Using indexer syntax dictionary[key] = value;
Using Add() Method with Key and Value
The most common approach is using the Add() method with separate key and value parameters −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<int, string> products = new Dictionary<int, string>();
// Add key-value pairs using Add() method
products.Add(1, "TVs");
products.Add(2, "Appliances");
products.Add(3, "Mobile");
products.Add(4, "Laptop");
// Display the dictionary
foreach (KeyValuePair<int, string> item in products) {
Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
}
}
}
The output of the above code is −
Key = 1, Value = TVs Key = 2, Value = Appliances Key = 3, Value = Mobile Key = 4, Value = Laptop
Using Add() Method with KeyValuePair
You can also add elements using the KeyValuePair structure −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
IDictionary<int, string> d = new Dictionary<int, string>();
d.Add(new KeyValuePair<int, string>(1, "TVs"));
d.Add(new KeyValuePair<int, string>(2, "Appliances"));
d.Add(new KeyValuePair<int, string>(3, "Mobile"));
d.Add(new KeyValuePair<int, string>(4, "Tablet"));
d.Add(new KeyValuePair<int, string>(5, "Laptop"));
foreach (KeyValuePair<int, string> ele in d) {
Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value);
}
}
}
The output of the above code is −
Key = 1, Value = TVs Key = 2, Value = Appliances Key = 3, Value = Mobile Key = 4, Value = Tablet Key = 5, Value = Laptop
Using Indexer Syntax
The indexer syntax provides a convenient way to add or update key-value pairs −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
Dictionary<string, int> ages = new Dictionary<string, int>();
// Add using indexer syntax
ages["Alice"] = 25;
ages["Bob"] = 30;
ages["Charlie"] = 35;
// Update existing key (no exception thrown)
ages["Alice"] = 26;
foreach (var person in ages) {
Console.WriteLine("{0} is {1} years old", person.Key, person.Value);
}
}
}
The output of the above code is −
Alice is 26 years old Bob is 30 years old Charlie is 35 years old
Comparison of Methods
| Method | Behavior with Duplicate Keys | Best Use Case |
|---|---|---|
| Add(key, value) | Throws ArgumentException | When you want to ensure no duplicates |
| Add(KeyValuePair) | Throws ArgumentException | Working with KeyValuePair objects |
| dictionary[key] = value | Overwrites existing value | When you want to add or update |
Conclusion
C# Dictionary offers multiple methods to add key-value pairs: Add() method for strict insertion, KeyValuePair approach for structured data, and indexer syntax for flexible add-or-update operations. Choose the method based on whether you need duplicate key protection or update capability.
