System.Collections.Generic.Dictionary

Added in .NET 2.0
This type of collection represents a collection of keys and values
Every value has a unique key associated with it.
Objects made available through unique keys.
You can indicate the data type of both the key and value at declaration
Items are stored through the related KeyValuePair class
DictionaryEntry is actually a KeyValuePair
This is built-in when you have a dictionary


System.Collections.Generic.Dictionary myDictionary; 
myDictionary = new System.Collections.Generic.Dictionary<string, System.DateTime>();

myDictionary.Add("Mon", new System.DateTime(2020,10,3));
myDictionary.Add("Tue", new System.DateTime(2021,10,3));
myDictionary.Add("Wed", new System.DateTime(2022,10,3));

if (myDictionary.ContainsKey("Tue")
{
   System.DateTime myDateTime;
   myDateTime = (System.DateTime)myDictionary["Tue"];
}

This is how you can access the keys and values
The entry in the dictionary is an instance of KeyValuePair<string, DateTime>.

foreach (System.Collections.Generic.KeyValuePair<string, System.DateTime> kvp in myDictionary) 
{
   string myText;
   myText = System.String.Format("Key={0}, Value={1}",kvp.Key, kvp.Value);
}

Print the Keys

System.Collections.Generic.Dictionary<string, System.DateTime>.KeyCollection myKeys; 
myKeys = myDictionary.Keys;
foreach (string myKey in myKeys)
{
   System.Console.WriteLine(myKey);
}

Print the Values

System.Collections.Generic.Dictionary<string, System.DateTime>.ValueCollection myValues; 
myValues = myDictionary.Values;
foreach (System.DateTime myValue in myValues)
{
   System.Console.WriteLine(myValue);
}

Different types of key,pairs

String Items - System.Collections.Generic.Dictionary(Of String, String) - These are referenced in code as Dictionary
String Array Items - System.Collections.Generic.Dictionary(Of String, String()) - These are referenced in code as Dictionary Arrays



Group of DataGridViewRows

System.Collections.Gernic.Dictionary<int, System.Windows.Forms.DataGridViewRow> dicRows; 

dicRows = new System.Collections.Generic.Dictionary<int, System.Windows.Forms.DataGridView>();
dicRows.Add(key,value)
dicRows.Add(objdatagridview.cells(0).rowindex, objdatagridviewrow)

Dim dicRows As System.Collections.Generic.Dictionary(Of Integer, System.Windows.Forms.DataGridViewRow) 

dicRows = New System.Collections.Generic.Dictionary(Of Integer, System.Windows.Forms.DataGridViewRow)
dicRows.Add(key,value)
dicRows.Add(objdatagridview.cells(0).rowindex, objdatagridviewrow)

Group of Objects

System.Collections.Generic.Dictionary<string, MyObject>(); 
dicObjects = new System.Collections.Generic.Dictionary<string, MyObject>();

You can also obtain a collection from the Dictionary
KeyCollection
ValueCollection


Does an Element Exist


If dicAuthors.Keys.Contains("002" 

If dicAuthors.ContainsKeys("002") = True Then

End If

Declaring



Initializing

Added in .NET 4.6
There is now another way which is a more readable format

public Dictionary <string, string> MyDictionary = new Dictionary<string, string>() 
{
   {"Russell", "London"}
   {"Steven","New York"}
}

instead of

public Dictionary <string, string> MyDictionary = new Dictionary<string, string>() 
{
   ["Russell"] = "London"
   ["Steven"] = "New York"
}

Items - Add / Remove



Counting Occurrences

System.Collections.Generic.Dictionary<string, int> MyDictionary; 
MyDictionary = new System.Collections.Generic.Dictionary<string, int>();

public static Dictionary_Counting_Add(
   System.Collections.Generic.Dictionary<string, int> MyDictionary,
   string myItem)
{
   int counter;

   try
   {
      if (MyDictionary.ContainsKey(myItem) == false)
      {
         counter = 1;
         MyDictionary.Add(myItem, counter);
      }
      else
      {
         counter = MyDictionary[myItem];
         MyDictionary[myItem] = counter++;
      }
   }
   catch (System.Exception ex)
   {
   }
}

public static Dictionary_Counting_FindMax(
   System.Collections.Generic.Dictionary<string, int> MyDictionary)
{
   int occurrences_max;
   int occurrences;
   string myTopItem;

   try
   {
      for (int icount = 0; icount < MyDictionary.Count; icount++)
      {
         occurrences = MyDictionary.ElementAt(icount).Value
         if (occurrences > occurrences_max)
         {
            occurrences_max = occurrences;
            myTopItem = MyDictionary.ElementAt(icount).Key
         }
      }
      return myTopItem;
   }
   catch (System.Exception ex)
   {
   }
}


Group of Strings

System.Collections.Generic.Dictionary<string, string> dicForFastAccess; 
dicAuthors = new System.Collections.Generic.Dictionary<string, string>();

dicForFastAccess.Add(key, value);
dicForFastAccess.Add("001", "my first string");
dicForFastAccess.Add("002", "my second string");

Group of Arrays

System.Collections.Generic.Dictionary<string, string[]> dicForStoringArrays; 
dicForStoringArrays = new System.Collections.Generic.Dictionary<string, string[]>();

dicForStoringArrays.Add(key,value);
dicForStoringArrays.Add("Item1", ??);


Removing


dicAuthors.Remove(key) 
dicAuthors.Remove("001")

Items - Updating


Items - Finding


Looping



foreach (System.Collections.Generic.KeyValuePair<int, string> objKeyValuePair in dicItems) 
{
   Debug.Print objKeyValuePair.Key + " - " + objKeyValuePair.Value;
}

for (int icount = 0; icount < MyDictionary.Count; icount++) 
{
   skey = MyDictionary.ElementAt(icount).Key
}

Converting to an array first

string snamesarray; 

snamesarray = dicAuthors.ToArray
for (int icount = 0; icount < MyDictionary.Count; icount++)
{
   snamesarray.GetValue(icount)
}

Sorting



System.Collections.Generic.IDictionary<TKey, TValue>

Added in .NET 2.0
This is the interface for a key/value pair collection.


It supports all the methods of ICollection<T> and IEnumerable<T>
Does the dictionary contain a particular key
Does the dictionary contain a particular key and value
Add entry
Remove entry
Get the value corresponding to a given key (indexing operator)
Returns all the keys
Returns all the values


You can check if a given key exists
You can check if a given key and value exists
You can add an entry pair to the dictionary
You can remove an entry from the dictionary
You can get the value corresponding to a given key
You can return the set of keys in the dictionary
You can set the values in the dictionary


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext