Dictionary Overview In C#

Dictionary in C#

  • The dictionary is a generic class that belongs to the System. Collection namespace in. NET.
  • The dictionary type in C# allows the user to retrieve a corresponding value very quickly for a specified key value.
  • A dictionary can store Keys and Values of any data type. 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 a Dictionary is associated with a corresponding Value.
  • Retrieving a value 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 corresponding to a key can be NULL and/or a 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 its key.
  • In the case of a multi-user environment, a Dictionary<TKey, TValue> can support multiple readers concurrently until the 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 a List type, but if have want to look up a value in a collection, then always choose Dictionary.
  • Has better performance than any other type if the number of elements is large in a collection.
  • Almost the same performance as that of an array, so sometimes an array and dictionary objects are interchangeable.

Methods and Attributes in C#


Declaration in C#

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() in C#

This method is used to add elements with key-value pairs 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 the Dictionary type represents a collection, we can use the foreach loop to go through all the items and read them using the Key and Value properties.

foreach (KeyValuePair<string, Int16> author in AuthorList)
{
    Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
}

Remove() in C#

This method is used to remove an element from a dictionary object.

DicObj.Remove("key4");

Clear() in C#

This method removes all the elements from a dictionary object.

DicObj.Clear();

ContainsKey() in C#

This method is used to find a key from the 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() in C#

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 in C#

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 in C#

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 in C#

The Item property is used to get and set the value associated with the specified key.

// Set Item value
DicObj["key4"] = value5;
// Get Item value
string value = DicObj["key4"];

Count in C#

This property is used to count the number of elements present in the dictionary at any time.

int noOfElements = DicObj.Count;


Recommended Free Ebook
Similar Articles