Posts

Showing posts with the label #C# #Dictionary

C# - Dictionary

The  Dictionary<TKey, TValue>  is a generic collection that stores key-value pairs in no particular order. Dictionary Characteristics Dictionary<TKey, TValue>  stores key-value pairs. Comes under  System.Collections.Generic  namespace. Implements  IDictionary<TKey, TValue>  interface. Keys must be unique and cannot be null. Values can be null or duplicate. Values can be accessed by passing associated key in the indexer e.g.  myDictionary[key] Elements are stored as  KeyValuePair<TKey, TValue>  objects. Creating a Dictionary You can create the  Dictionary<TKey, TValue>  object by passing the type of keys and values it can store. The following example shows how to create a dictionary and add key-value pairs. Example: Create Dictionary and Add Elements IDictionary < int , string > numberNames = new Dictionary < int , string >(); numberNames.Add(1, "One" ); //adding a key/value using the A...