Dictionary in C# :
- Dictionary is a generic class that belongs to System.Collection namespace in .NET.
- The dictionary type in C# allow user to retrieve corresponding value very fast with key value.
- A dictionary can store Keys and Values of any data type in .NET.
- It provides a mapping from a set of keys to a set of values .
- Represented as Dictionary<TKey, TValue> pair where TKey represents Key and TValue represents the value associated with the key.
- Each Key in dictionary is associated with corresponding Value.
- 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.
- Each key in the dictionary must be unique and cannot be NULL but a value could be NULL and duplicate.
- The capacity of a Dictionary<TKey, TValue> varies according to the number of elements present in the Dictionary.
- As elements are added to a Dictionary<TKey, TValue>, the capacity is automatically increased as required by reallocating the internal array.
- The maximum capacity of a dictionary is up to 2 billion elements on a 64-bit system by setting the enabled attribute of the gcAllowVeryLargeObjects configuration element to true in the run-time environment.
- Each item in the Dictionary can be iterated through KeyValuePair<TKey, TValue> structure representing a value and it’s key.
- In case of multiuser environment a Dictionary<TKey, TValue> can support multiple readers concurrently until collection is modified even though enumerating through a collection is intrinsically not a thread-safe procedure.
- In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration.
- So if you have to iterate through each member of the collection use List type but if have want to look up a value in a collection then always choose Dictionary.
- Having better performance than any other type if no of elements is large in a collection.
- Almost same performance as that of an array so sometimes an array and dictionary objects are interchangeable (ref http://www.dotnetperls.com/array-dictionary-test).
Methods and Attributes with Example
- Declaration: A dictionary can be declared as follows
//If capacity of dictionary is not known
Dictionary<string, string> dicObj = new Dictionary<string, string>();
//If capacity of dictionary is known
Dictionary<string, string> dicObj = new Dictionary<string, string>(5);
- Add(): This method is used to add elements with key value pair in the dictionary object.
DicObj.Add(“key1”, “val1”);
DicObj.Add(“key2”, ” val2″);
DicObj.Add(“key3″, ” val3″);
DicObj.Add(“key4″, ” val4″);
- Reading or iterating through Dictionary element: Since Dictionary type represents a collection so we can use the foreach loop to go through all the items and read them using they Key and Value properties.
foreach (KeyValuePair<string, Int16> author in AuthorList)
{
Console.WriteLine(“Key: {0}, Value: {1}”, author.Key, author.Value);
}
- Remove(): This method is used to remove an element from dictionary object.
DicObj.Remove(“key4“);
- Clear(): This method removes all the elements from a dictionary object.
DicObj.Clear();
- ContainsKey(): This method is used to find a key from dictionary and it returns a Boolean value indicating whether that key is found in the collection or not.
if (!AuthorList.ContainsKey(“key4”))
{
DicObj [“key4”] = 20;
}
- ContainsValue(): This method is used to check whether a value exists in the dictionary or not and it returns a Boolean value indicating whether that value is found in the collection or not.
if (!AuthorList.ContainsValue(“Value4”))
{
// write code here
}
- Keys : This attribute returns a collection of keys present in the dictionary object. It returns an object of KeyCollection type.
Dictionary<string, string>.KeyCollection keys = DicObj.Keys;
foreach (string key in keys)
{
// code to process keys
}
- Values : This attribute returns a collection of values present in the dictionary object. It returns an object of ValueCollection type.
Dictionary<string, string>.ValueCollection values = DicObj.Values;
foreach (string val in values)
{
// code to process values
}
- Item: The Item property is used to gets and sets the value associated with the specified key.
// Set Item value
DicObj [“key4”] = value5;
// Get Item value
string value = DicObj [“key4]
- Count : This property is used to count no of elements present in the dictionary at any time.
Int noOfElements = DicObj.Count
Advertisements