

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dictionary Class in C#
Dictionary in C# is a collection of keys and values. It is a generic collection class in the System.Collection.Generics namespace.
Syntax
Following is the syntax −
public class Dictionary<TKey,TValue>
Above, the key parameter is the type of keys in the dictionary, whereas TValue is the type of values.
Example
Let us now create a Dictionary and add some elements −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("One", "John"); dict.Add("Two", "Tom"); dict.Add("Three", "Jacob"); dict.Add("Four", "Kevin"); dict.Add("Five", "Nathan"); Console.WriteLine("Key/value pairs..."); foreach(KeyValuePair<string, string> res in dict){ Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); } } }
Output
This will produce the following output −
Key/value pairs... Key = One, Value = John Key = Two, Value = Tom Key = Three, Value = Jacob Key = Four, Value = Kevin Key = Five, Value = Nathan
Example
Now, let us see an example and remove some keys −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("One", "Kagido"); dict.Add("Two", "Ngidi"); dict.Add("Three", "Devillers"); dict.Add("Four", "Smith"); dict.Add("Five", "Warner"); Console.WriteLine("Count of elements = "+dict.Count); Console.WriteLine("Removing some keys..."); dict.Remove("Four"); dict.Remove("Five"); Console.WriteLine("Count of elements (updated) = "+dict.Count); Console.WriteLine("\nKey/value pairs..."); foreach(KeyValuePair<string, string> res in dict){ Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); } Console.Write("\nAll the keys..\n"); Dictionary<string, string>.KeyCollection allKeys = dict.Keys; foreach(string str in allKeys){ Console.WriteLine("Key = {0}", str); } } }
Output
This will produce the following output −
Count of elements = 5 Removing some keys... Count of elements (updated) = 3 Key/value pairs... Key = One, Value = Kagido Key = Two, Value = Ngidi Key = Three, Value = Devillers All the keys.. Key = One Key = Two Key = Three
- Related Questions & Answers
- The Dictionary Class in Javascript
- Importance of a Dictionary class in Java?
- Convert string dictionary to dictionary in Python
- Python Program to Form a Dictionary from an Object of a Class
- Python - Convert flattened dictionary into nested dictionary
- Python Convert nested dictionary into flattened dictionary?
- Append Dictionary Keys and Values (In order ) in dictionary using Python
- How to define a Python dictionary within dictionary?
- How to convert Javascript dictionary to Python dictionary?
- Data Dictionary in DBMS
- Dictionary Methods in C#
- Dictionary Methods in Python
- Dictionary Methods in Java
- Updating Dictionary in Python
- Alien Dictionary in C++
Advertisements