08 Aug C# Dictionary
The Dictionary class is defined in the System.Collections.Generics namespace. It stores the key-value pair and implements the Implements IDictionary<TKey, TValue> interface. The key in a Dictionary is unique and can never be null. The result will not be sorted by keys, unlike the SortedList Collection.
Create a Dictionary
To create a Dictionary with an int key and string value pair, use <int, string>. Set the type in angular braces as shown below:
1 2 3 |
Dictionary<int, string> dict = new Dictionary<int, string>(); |
Let us now see an example of creating a Dictionary in C# with string elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(4,"Badminton"); dict.Add(1,"Cricket"); dict.Add(5,"Golf"); dict.Add(7,"VolleyBall"); dict.Add(2,"Football"); dict.Add(3,"Tennis"); dict.Add(6,"Hockey"); Console.WriteLine("\nDictionary..."); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
Dictionary... Key: 4, Value: Badminton Key: 1, Value: Cricket Key: 5, Value: Golf Key: 7, Value: VolleyBall Key: 2, Value: Football Key: 3, Value: Tennis Key: 6, Value: Hockey |
Remove all the keys and values
To remove all the keys and values from a Dictionary, use the Clear() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(4,"Badminton"); dict.Add(1,"Cricket"); dict.Add(5,"Golf"); dict.Add(7,"VolleyBall"); dict.Add(2,"Football"); dict.Add(3,"Tennis"); dict.Add(6,"Hockey"); Console.WriteLine("\nDictionary..."); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } // Display the count of Dictionary elements Console.WriteLine("Count = "+dict.Count); // Clearing the elements dict.Clear(); Console.WriteLine("\nRemoving all the elements..."); Console.WriteLine("Count (Updated) = "+dict.Count); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Dictionary... Key: 4, Value: Badminton Key: 1, Value: Cricket Key: 5, Value: Golf Key: 7, Value: VolleyBall Key: 2, Value: Football Key: 3, Value: Tennis Key: 6, Value: Hockey Count = 7 Removing all the elements... Count (Updated) = 0 |
Display only the keys
Use the Keys property to display only the keys from a Dictionary in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(4,"Badminton"); dict.Add(1,"Cricket"); dict.Add(5,"Golf"); dict.Add(7,"VolleyBall"); dict.Add(2,"Football"); dict.Add(3,"Tennis"); dict.Add(6,"Hockey"); Console.WriteLine("\nDictionary..."); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nDictionary Keys..."); List<int> k = new List<int>(dict.Keys); foreach (int keys in k) { Console.WriteLine(keys); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Dictionary... Key: 4, Value: Badminton Key: 1, Value: Cricket Key: 5, Value: Golf Key: 7, Value: VolleyBall Key: 2, Value: Football Key: 3, Value: Tennis Key: 6, Value: Hockey Dictionary Keys... 4 1 5 7 2 3 6 |
Display only the values
Use the Values property to display only the values from a Dictionary in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(4,"Badminton"); dict.Add(1,"Cricket"); dict.Add(5,"Golf"); dict.Add(7,"VolleyBall"); dict.Add(2,"Football"); dict.Add(3,"Tennis"); dict.Add(6,"Hockey"); Console.WriteLine("\nDictionary..."); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nDictionary Values..."); List<string> v = new List<string>(dict.Values); foreach (string s in v) { Console.WriteLine(s); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Dictionary... Key: 4, Value: Badminton Key: 1, Value: Cricket Key: 5, Value: Golf Key: 7, Value: VolleyBall Key: 2, Value: Football Key: 3, Value: Tennis Key: 6, Value: Hockey Dictionary Values... Badminton Cricket Golf VolleyBall Football Tennis Hockey |
Check the Dictionary for a specific key
The ContainsKey() method allows you to check a Dictionary for a key. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(4,"Badminton"); dict.Add(1,"Cricket"); dict.Add(5,"Golf"); dict.Add(7,"VolleyBall"); dict.Add(2,"Football"); dict.Add(3,"Tennis"); dict.Add(6,"Hockey"); Console.WriteLine("\nDictionary..."); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nKey 2 is present? "+dict.ContainsKey(2)); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Dictionary... Key: 4, Value: Badminton Key: 1, Value: Cricket Key: 5, Value: Golf Key: 7, Value: VolleyBall Key: 2, Value: Football Key: 3, Value: Tennis Key: 6, Value: Hockey Key 2 is present? True |
Check the Dictionary for a specific value
The ContainsValue() method allows you to check a Dictionary for a value. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Collections; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { Dictionary<int, string> dict = new Dictionary<int, string>(); dict.Add(4,"Badminton"); dict.Add(1,"Cricket"); dict.Add(5,"Golf"); dict.Add(7,"VolleyBall"); dict.Add(2,"Football"); dict.Add(3,"Tennis"); dict.Add(6,"Hockey"); Console.WriteLine("\nDictionary..."); foreach (KeyValuePair<int, string> i in dict) { Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value); } Console.WriteLine("\nValue Tennis is present? "+dict.ContainsValue("Tennis")); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Dictionary... Key: 4, Value: Badminton Key: 1, Value: Cricket Key: 5, Value: Golf Key: 7, Value: VolleyBall Key: 2, Value: Football Key: 3, Value: Tennis Key: 6, Value: Hockey Value Tennis is present? True |
Read More
No Comments