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.
- IDictionary<int, string> NumberDictionary = new Dictionary<int, string>();
The following code snippet adds items to the dictionary.
- NumberDictionary.Add(1, "One");
- NumberDictionary.Add(2, "Two");
- 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.
- Console.WriteLine("--Number Dictionaty--");
-
- foreach (KeyValuePair<int , string> item in NumberDictionary)
- {
- Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
- }
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>.
- IDictionary<char,List<string>> ContryNameDictionary = new Dictionary<char, List<string>>();
The following code snippet creates a list where collection of country name alphabet wise.
- List<string> ContryNameStartWith_A = new List<string> { "Afghanistan", "Albania", "Algeria", "Andorra" };
- List<string> ContryNameStartWith_B = new List<string> { "Bahamas", "Bahrain", "Bangladesh", "Barbados" };
- List<string> ContryNameStartWith_C = new List<string> { "Cambodia", "Cameroon", "Canada", "Cape Verde" };
The following code snippet adds items to the dictionary.
- ContryNameDictionary.Add('A', ContryNameStartWith_A);
- ContryNameDictionary.Add('B', ContryNameStartWith_B);
- 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.
- Console.WriteLine("--Alphabet Wise Country Name Dictionaty--");
-
- foreach (KeyValuePair<char, List<string>> item in ContryNameDictionary)
- {
- Console.WriteLine($"Alphabet: {item.Key}, --> Contry Name: {string.Join(", ",ContryNameDictionary[item.Key])}");
- }
Output
Example 3
The following code snippet creates a user-defined class, StudentInfo.
- public class StudentInfo
- {
- public string Name { get; set; }
- public string Gender { get; set; }
- public string Standard { get; set; }
- public int Total { get; set; }
- }
The following code snippet creates a dictionary where the key type is int(Student Roll Number) and value type is user-defined class StudentInfo.
- IDictionary<int, StudentInfo> StudentInfoDictionary = new Dictionary<int, StudentInfo>();
The following code snippet creates a list where collection of student information.
- StudentInfo student1 = new StudentInfo() { Name = "Emma", Gender = "Female", Standard = "10", Total =506};
- StudentInfo student2 = new StudentInfo() { Name = "Liam", Gender = "Male", Standard = "12", Total = 600 };
- StudentInfo student3 = new StudentInfo() { Name = "Noah", Gender = "Male", Standard = "09", Total = 550 };
- StudentInfo student4 = new StudentInfo() { Name = "Olivia", Gender = "Female", Standard = "08", Total = 685 };
- StudentInfo student5 = new StudentInfo() { Name = "Ava", Gender = "Female", Standard = "11", Total = 605 };
- StudentInfo student6 = new StudentInfo() { Name = "Oliver", Gender = "Male", Standard = "10", Total = 700 };
The following code snippet adds items to the dictionary.
- StudentInfoDictionary.Add(1, student1);
- StudentInfoDictionary.Add(2, student2);
- StudentInfoDictionary.Add(3, student3);
- StudentInfoDictionary.Add(4, student4);
- StudentInfoDictionary.Add(5, student5);
- 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.
- Console.WriteLine("--Student Information Dictionaty--");
-
- foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)
- {
- Console.WriteLine($"Roll No: {item.Key}");
- Console.Write($"Name: {StudentInfoDictionary[item.Key].Name}");
- Console.Write($", Gender: {StudentInfoDictionary[item.Key].Gender}");
- Console.Write($", Standard: {StudentInfoDictionary[item.Key].Standard}");
- Console.WriteLine($", Total: {StudentInfoDictionary[item.Key].Total}");
- Console.WriteLine("-------------------------------------------------------");
- }
Output
Example - ContainsKey() & Contains()
Determines whether the Dictionary<TKey,TValue> contains the specified key and contains.
- Console.WriteLine($"Example: ContainsKey() & Contains()");
- Console.WriteLine($"StudentInfoDictionary have key 5 = {StudentInfoDictionary.ContainsKey(5)}");
- Console.WriteLine($"StudentInfoDictionary have key 7 = {StudentInfoDictionary.ContainsKey(7)}");
- Console.WriteLine($"StudentInfoDictionary have Contains key=1 and value=student1 result = {StudentInfoDictionary.Contains(new KeyValuePair<int, StudentInfo>(1, student1))}");
- Console.WriteLine($"StudentInfoDictionary have Contains key=4 and value=student2 result = {StudentInfoDictionary.Contains(new KeyValuePair<int, StudentInfo>(4, student2))}");
Output
Example - TryGetValue()
Gets the value associated with the specified key.
- StudentInfo result;
-
- if (StudentInfoDictionary.TryGetValue(4, out result))
- {
- Console.WriteLine($"Example: TryGetValue()");
- Console.Write($"Name: {result.Name}");
- Console.Write($", Gender: {result.Gender}");
- Console.Write($", Standard: {result.Standard}");
- Console.WriteLine($", Total: {result.Total}");
- }
- else
- {
- Console.WriteLine("Could not find the specified key.");
- }
Output
Example - Remove elements from Dictionary
Removes the value with the specified key from the Dictionary<TKey,TValue>.
- Console.WriteLine("--Before Remove Key 2--");
- foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)
- {
- Console.WriteLine($"Roll No: {item.Key}, Name: {StudentInfoDictionary[item.Key].Name}");
- }
-
- StudentInfoDictionary.Remove(2);
-
- Console.WriteLine("-- StudentInfoDictionary.Remove(2);--");
- Console.WriteLine("--After Remove Key 2 and Befoer Remove Key=3 and Value=Student3 --");
-
- foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)
- {
- Console.WriteLine($"Roll No: {item.Key}, Name: {StudentInfoDictionary[item.Key].Name}");
- }
-
- StudentInfoDictionary.Remove(new KeyValuePair<int, StudentInfo>(3, student3));
- Console.WriteLine("StudentInfoDictionary.Remove(new KeyValuePair<int, StudentInfo>(3, student3));");
-
- foreach (KeyValuePair<int, StudentInfo> item in StudentInfoDictionary)
- {
- Console.WriteLine($"Roll No: {item.Key}, Name: {StudentInfoDictionary[item.Key].Name}");
- }
Output