Topics:
- Generic Dictionary class
- SortedDictionary class
Given a word, a dictionary provides one or more meanings of the word. The word which meaning to be looked up in the dictionary is unique but the meaning of the word can be numerous. Let's consider the word bark, it has multiple meanings in the dictionary. For example, different meanings of bark
- I hope her dog doesn’t bark when I knock on the door.
- The tree bark is rough to the touch.
- I love eating pretzels covered with almond bark.
Source: https://grammar.yourdictionary.com/for-students-and-parents/words-with-multiple-meanings.html
Similarly, C# Dictionary generic class has unique key which has associated value(s). Dictionary represents a collection of keys and values, where each key in the dictionary is unique. It is symbolized like Dictionary<Tkey, Tvalue> where Tkey is called key parameter and Tvalue is called value parameter of Dictionary class respectively. Dictionary class belongs to System.Collections.Generic namespace. Key is unique in a dictionary and adding same key again will throw exception.
Note: The Dictionary<TKey,TValue> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary<TKey,TValue> class is implemented as a hash table.
Example
using System;
using System.Collections.Generic;
namespace ConsoleDictionary
{
class Program
{
static void Main(string[] args)
{
Dictionary dict = new Dictionary();
dict.Add(1, "Ajeet");//key, value parameters in order
dict.Add(2, "Amit");
dict.Add(3, "Ajay");
dict.Add(4, "Anand");
dict.Add(5, "Anant");
// Other Methods are Remove, Clear, ContainsKey, ContainsValue, TryGetValue etc.
foreach (KeyValuePair item in dict)
{
Console.WriteLine(item);
//Console.WriteLine("{0} {1}", item.Key, item.Value);
}
//Properties
Console.WriteLine(dict.Count) ;//5
Console.WriteLine(dict.Keys.Count) ;//5
Console.WriteLine(dict.Values.Count) ;//5
Console.ReadKey();
}
}
}
Dictionary is type safe so the datatypes of key and value must match when an item is added in the dictionary.
Note The output will be as per added order of keys.
KeyValuePair class
Since the Dictionary<TKey,TValue> is a collection of keys and values, the element type is not the type of the key or the type of the value. Instead, the element type is a KeyValuePair<TKey,TValue> of the key type and the value type.
The foreach statement is a wrapper around the enumerator, which allows only reading from the collection, not writing to it.
SortedDictionary class Example
using System;
using System.Collections.Generic;
namespace ConsoleDictionary
{
class Program
{
static void Main(string[] args)
{
SortedDictionary dict = new SortedDictionary();
dict.Add(1, "Ajeet");
dict.Add(4, "Amit");
dict.Add(3, "Ajay");
dict.Add(2, "Anand");
dict.Add(5, "Anant");
foreach (KeyValuePair item in dict)
{
Console.WriteLine("{0} {1}",item.Key, item.Value);
}
//Properties
Console.WriteLine(dict.Count) ;//5
Console.WriteLine(dict.Keys.Count) ;//5
Console.WriteLine(dict.Values.Count) ;//5
Console.ReadKey();
}
}
}
NOTE The output will be as per sorted keys, not as per added order.
1 Ajeet2 Anand
3 Ajay
4 Amit
5 Anant
5
5
5
No comments:
Post a Comment