
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Add key-value pair in C# Dictionary
To add key-value pair in C# Dictionary, firstly declare a Dictionary.
IDictionary<int, string> d = new Dictionary<int, string>();
Now, add elements with KeyValuePair.
d.Add(new KeyValuePair<int, string>(1, "TVs")); d.Add(new KeyValuePair<int, string>(2, "Appliances")); d.Add(new KeyValuePair<int, string>(3, "Mobile"));
After adding elements, let us display the key-value pair.
Example
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")); d.Add(new KeyValuePair<int, string>(6, "Desktop")); d.Add(new KeyValuePair<int, string>(7, "Hard Drive")); d.Add(new KeyValuePair<int, string>(8, "Flash Drive")); foreach (KeyValuePair<int, string> ele in d) { Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value); } } }
Output
Key = 1, Value = TVs Key = 2, Value = Appliances Key = 3, Value = Mobile Key = 4, Value = Tablet Key = 5, Value = Laptop Key = 6, Value = Desktop Key = 7, Value = Hard Drive Key = 8, Value = Flash Drive
- Related Articles
- Add a key value pair to dictionary in Python
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
- Appending a key value pair to an array of dictionary based on a condition in JavaScript?
- Accessing Key-value in a Python Dictionary
- Get key from value in Dictionary in Python
- Get key with maximum value in Dictionary in Python
- JavaScript - Sort key value pair object based on value?
- What is possible key/value delimiter in Python dictionary?
- JavaScript - Convert an array to key value pair
- Python Program to print key value pairs in a dictionary
- Add a value to Pair Tuple in Java
- C++ Program to Update value of Dictionary using key
- Add key and value into StringDictionary in C#
- Java Program to remove key value pair from HashMap?

Advertisements