C# | ListDictionary Class - GeeksforGeeks

24 Jan, 2019
ListDictionary is a specialized collection. It comes under the System.Collections.Specialized namespace. This type represents a non-generic dictionary type. It is implemented with a linked list. This class is a simple implementation of a dictionary collection (System.Collections.IDictionary) for small lists. It implements the IDictionary methods and properties, and is suggested for use with a small number of elements (less than 10).
Characteristics of ListDictionary Class:
ListDictionary is a simple implementation of IDictionary using a singly linked list.
It is smaller and faster than a Hashtable if the number of elements is 10 or less.
ListDictionary should not be used if performance is important for large numbers of elements.
Items in a ListDictionary are not in any guaranteed order.
A key cannot be null, but a value can.
Example:
// C# code to create a ListDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {  // Driver codepublic static void Main(){      // Creating a ListDictionary named myDict    ListDictionary myDict = new ListDictionary();      // Adding key/value pairs in myDict    myDict.Add("Australia", "Canberra");    myDict.Add("Belgium", "Brussels");    myDict.Add("Netherlands", "Amsterdam");    myDict.Add("China", "Beijing");    myDict.Add("Russia", "Moscow");    myDict.Add("India", "New Delhi");      // To get count of key/value pairs in myDict    Console.WriteLine("Total key/value pairs in myDict are : " + myDict.Count);          // Displaying the key/value pairs in myDict    Console.WriteLine("The key/value pairs in myDict are : ");      foreach(DictionaryEntry de in myDict)    {        Console.WriteLine(de.Key + " " + de.Value);    }}}
Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi

Example 1:
// C# code to get the number// of key/value pairs contained// in the ListDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver code    public static void Main()    {          // Creating a ListDictionary named myDict        ListDictionary myDict = new ListDictionary();          // Adding key/value pairs in myDict        myDict.Add("Australia", "Canberra");        myDict.Add("Belgium", "Brussels");        myDict.Add("Netherlands", "Amsterdam");        myDict.Add("China", "Beijing");        myDict.Add("Russia", "Moscow");        myDict.Add("India", "New Delhi");          // Displaying the number of key/value        // pairs contained in the ListDictionary        Console.WriteLine(myDict.Count);    }}
6

Example 2:
// C# code to check if ListDictionary is read-onlyusing System;using System.Collections;using System.Collections.Specialized;  class GFG {      // Driver code    public static void Main()    {          // Creating a ListDictionary named myDict        ListDictionary myDict = new ListDictionary();          myDict.Add("Australia", "Canberra");        myDict.Add("Belgium", "Brussels");        myDict.Add("Netherlands", "Amsterdam");        myDict.Add("China", "Beijing");        myDict.Add("Russia", "Moscow");        myDict.Add("India", "New Delhi");          // Checking if ListDictionary is read-only        Console.WriteLine(myDict.IsReadOnly);    }}
False

Example 1:
// C# code to add an entry with// the specified key and value// into the ListDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {  // Driver codepublic static void Main(){      // Creating a ListDictionary named myDict    ListDictionary myDict = new ListDictionary();      myDict.Add("Australia", "Canberra");    myDict.Add("Belgium", "Brussels");    myDict.Add("Netherlands", "Amsterdam");    myDict.Add("China", "Beijing");    myDict.Add("Russia", "Moscow");    myDict.Add("India", "New Delhi");      // Displaying the total number of elements in myDict    Console.WriteLine("Total number of elements in myDict are : "                                                   + myDict.Count);      // Displaying the elements in ListDictionary myDict    foreach(DictionaryEntry de in myDict)    {        Console.WriteLine(de.Key + " " + de.Value);    }}}
Total number of elements in myDict are : 6
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi

Example 2:
// C# code to remove all entries// from the ListDictionaryusing System;using System.Collections;using System.Collections.Specialized;  class GFG {  // Driver codepublic static void Main(){      // Creating a ListDictionary named myDict    ListDictionary myDict = new ListDictionary();      // Adding key/value pairs in myDict    myDict.Add("I", "first");    myDict.Add("II", "second");    myDict.Add("III", "third");    myDict.Add("IV", "fourth");    myDict.Add("V", "fifth");      // To get count of key/value pairs in myDict    Console.WriteLine("Total key/value pairs in myDict are : "                                               + myDict.Count);                                                    // Displaying the key/value pairs in myDict      Console.WriteLine("The key/value pairs in myDict are : ");      foreach(DictionaryEntry de in myDict)    {        Console.WriteLine(de.Key + " " + de.Value);    }      // Removing all entries from the ListDictionary    myDict.Clear();      // To get count of key/value pairs in myDict    Console.WriteLine("Total key/value pairs in myDict are : "                                               + myDict.Count);      // Displaying the key/value pairs in myDict      Console.WriteLine("The key/value pairs in myDict are : ");      foreach(DictionaryEntry de in myDict)    {        Console.WriteLine(de.Key + " " + de.Value);    }}}
Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
I first
II second
III third
IV fourth
V fifth
Total key/value pairs in myDict are : 0
The key/value pairs in myDict are :

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.listdictionary?view=netframework-4.7.2
CSharp-Specialized-ListDictionary
CSharp-Specialized-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Delegates
C# | Method Overriding
C# | Abstract Classes
Difference between Ref and Out keywords in C#
C# | Class and Object
Extension Method in C#
C# | Constructors
C# | Replace() Method
Introduction to .NET Framework
C# | Data Types