C# Dictionary <TKey, TValue>

Type Parameters

  • TKey
    The type of keys in the dictionary.

  • TValue
    The type of the values in the dictionary.
The Dictionary<TKey,TValue> is a member of "System.Collections.Generic" namespace it's a generic collection which is generally used to store key/value pairs data. Each and every key must be unique in the dictionary.
 
Example
 
Dictionary Initialization.
 
Dictionary<string,string> dictionary= newDictionary<string,string>();
 
Example 1
 
Creates a dictionary and Retrieve Items of a Dictionary in C#.
 
The following code snippet creates a dictionary where the key type is int and value type is a string.
  1. IDictionary<intstring> NumberDictionary = new Dictionary<intstring>();  
The following code snippet adds items to the dictionary.
  1. NumberDictionary.Add(1, "One");   
  2. NumberDictionary.Add(2, "Two");   
  3. NumberDictionary.Add(3, "Three");   
The Dictionary is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.
  1. Console.WriteLine("--Number Dictionaty--");  
  2.   
  3. foreach (KeyValuePair<int , string> item in NumberDictionary)   
  4. {   
  5.    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");   
  6. }   
Output

C# Dictionary <TKey, TValue />
 
Example 2
 
Creates a dictionary and Retrieve Items of a Dictionary in C#.
 
The following code snippet creates a dictionary where the key type is char and value type is List<string>.
  1. IDictionary<char,List<string>> ContryNameDictionary = new Dictionary<char, List<string>>();  
The following code snippet creates a list where collection of country name alphabet wise.
  1. List<string> ContryNameStartWith_A = new List<string> { "Afghanistan""Albania""Algeria""Andorra" }; 
  2. List<string> ContryNameStartWith_B = new List<string> { "Bahamas""Bahrain""Bangladesh""Barbados" };
  3. List<string> ContryNameStartWith_C = new List<string> { "Cambodia""Cameroon""Canada""Cape Verde" };   
The following code snippet adds items to the dictionary.
  1. ContryNameDictionary.Add('A', ContryNameStartWith_A);   
  2. ContryNameDictionary.Add('B', ContryNameStartWith_B);   
  3. ContryNameDictionary.Add('C', ContryNameStartWith_C);  
The Dictionary is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.
  1. Console.WriteLine("--Alphabet Wise Country Name Dictionaty--");  
  2.   
  3. foreach (KeyValuePair<char, List<string>> item in ContryNameDictionary)   
  4. {   
  5.    Console.WriteLine($"Alphabet: {item.Key}, --> Contry Name: {string.Join("",ContryNameDictionary[item.Key])}");   
  6. }   
Output
 
C# Dictionary <TKey, TValue />
 
Example 3
 
The following code snippet creates a user-defined class, StudentInfo.
  1. public class StudentInfo   
  2. {   
  3.         public string Name { getset; }   
  4.         public string Gender { getset; }   
  5.         public string Standard { getset; }   
  6.         public int Total { getset; }   
  7. }   
The following code snippet creates a dictionary where the key type is int(Student Roll Number) and value type is user-defined class StudentInfo.
  1. IDictionary<int, StudentInfo> StudentInfoDictionary = new Dictionary<int, StudentInfo>();  
The following code snippet creates a list where collection of student information.
  1. StudentInfo student1 = new StudentInfo() { Name = "Emma", Gender = "Female", Standard = "10", Total =506};   
  2. StudentInfo student2 = new StudentInfo() { Name = "Liam", Gender = "Male", Standard = "12", Total = 600 };   
  3. StudentInfo student3 = new StudentInfo() { Name = "Noah", Gender = "Male", Standard = "09", Total = 550 };   
  4. StudentInfo student4 = new StudentInfo() { Name = "Olivia", Gender = "Female", Standard = "08", Total = 685 };   
  5. StudentInfo student5 = new StudentInfo() { Name = "Ava", Gender = "Female", Standard = "11", Total = 605 };   
  6. StudentInfo student6 = new StudentInfo() { Name = "Oliver", Gender = "Male", Standard = "10", Total = 700 };  
The following code snippet adds items to the dictionary.
  1. StudentInfoDictionary.Add(1, student1);   
  2. StudentInfoDictionary.Add(2, student2);   
  3. StudentInfoDictionary.Add(3, student3);   
  4. StudentInfoDictionary.Add(4, student4);   
  5. StudentInfoDictionary.Add(5, student5);   
  6. StudentInfoDictionary.Add(6, student6);   
The Dictionary is a collection. We can use the foreach loop to go through all the items and read them using they Key and Value properties.
  1. Console.WriteLine("--Student Information Dictionaty--");  
  2.   
  3. foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)   
  4. {   
  5.    Console.WriteLine($"Roll No: {item.Key}");   
  6.    Console.Write($"Name: {StudentInfoDictionary[item.Key].Name}");   
  7.    Console.Write($", Gender: {StudentInfoDictionary[item.Key].Gender}");   
  8.    Console.Write($", Standard: {StudentInfoDictionary[item.Key].Standard}");   
  9.    Console.WriteLine($", Total: {StudentInfoDictionary[item.Key].Total}");   
  10.    Console.WriteLine("-------------------------------------------------------");   
  11. }  
Output
 
C# Dictionary <TKey, TValue />
 
Example - ContainsKey() & Contains()
 
Determines whether the Dictionary<TKey,TValue> contains the specified key and contains.
  1. Console.WriteLine($"Example: ContainsKey() & Contains()");
  2. Console.WriteLine($"StudentInfoDictionary have key 5 = {StudentInfoDictionary.ContainsKey(5)}");
  3. Console.WriteLine($"StudentInfoDictionary have key 7 = {StudentInfoDictionary.ContainsKey(7)}");
  4. Console.WriteLine($"StudentInfoDictionary have Contains key=1 and value=student1 result = {StudentInfoDictionary.Contains(new KeyValuePair<int, StudentInfo>(1, student1))}");
  5. Console.WriteLine($"StudentInfoDictionary have Contains key=4 and value=student2 result = {StudentInfoDictionary.Contains(new KeyValuePair<int, StudentInfo>(4, student2))}");
Output
 
C# Dictionary <TKey, TValue />
 
Example - TryGetValue() 
 
Gets the value associated with the specified key.
  1. StudentInfo result;   
  2.   
  3. if (StudentInfoDictionary.TryGetValue(4, out result))   
  4. {   
  5.    Console.WriteLine($"Example: TryGetValue()");   
  6.    Console.Write($"Name: {result.Name}");   
  7.    Console.Write($", Gender: {result.Gender}");   
  8.    Console.Write($", Standard: {result.Standard}");   
  9.    Console.WriteLine($", Total: {result.Total}");   
  10. }   
  11. else   
  12. {   
  13.    Console.WriteLine("Could not find the specified key.");   
  14. }   
Output
 
C# Dictionary <TKey, TValue />
 
Example - Remove elements from Dictionary
 
Removes the value with the specified key from the Dictionary<TKey,TValue>.
  1. Console.WriteLine("--Before Remove Key 2--");   
  2. foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)   
  3. {   
  4.    Console.WriteLine($"Roll No: {item.Key}, Name: {StudentInfoDictionary[item.Key].Name}");   
  5. }   
  6.   
  7. StudentInfoDictionary.Remove(2);   
  8.   
  9. Console.WriteLine("-- StudentInfoDictionary.Remove(2);--");   
  10. Console.WriteLine("--After Remove Key 2 and Befoer Remove Key=3 and Value=Student3 --");   
  11.   
  12. foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)   
  13. {    
  14.    Console.WriteLine($"Roll No: {item.Key}, Name: {StudentInfoDictionary[item.Key].Name}");   
  15. }   
  16.   
  17. StudentInfoDictionary.Remove(new KeyValuePair<int, StudentInfo>(3, student3));   
  18. Console.WriteLine("StudentInfoDictionary.Remove(new KeyValuePair<int, StudentInfo>(3, student3));");   
  19.   
  20. foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)   
  21. {   
  22.    Console.WriteLine($"Roll No: {item.Key}, Name: {StudentInfoDictionary[item.Key].Name}");   
  23. }  
Output
 
 
Want to learn more? Here is a detailed tutorial: Dictionary in C# 


Similar Articles