ListDictionary Class in C#

The ListDictionary class in C# implements the IDictionary interface using a singly linked list. It is optimized for small collections and is recommended for collections that typically contain fewer than 10 items. For larger collections, it's better to use Hashtable or Dictionary<TKey, TValue> for better performance.

Syntax

Following is the syntax for creating a ListDictionary

ListDictionary dict = new ListDictionary();
dict.Add(key, value);

Key Properties

Property Description
Count Gets the number of key/value pairs contained in the ListDictionary.
IsFixedSize Gets a value indicating whether the ListDictionary has a fixed size.
IsReadOnly Gets a value indicating whether the ListDictionary is read-only.
IsSynchronized Gets a value indicating whether the ListDictionary is synchronized (thread safe).
Item[Object] Gets or sets the value associated with the specified key.
Keys Gets an ICollection containing the keys in the ListDictionary.
Values Gets an ICollection containing the values in the ListDictionary.

Common Methods

Method Description
Add(Object, Object) Adds an entry with the specified key and value into the ListDictionary.
Clear() Removes all entries from the ListDictionary.
Contains(Object) Determines whether the ListDictionary contains a specific key.
CopyTo(Array, Int32) Copies the ListDictionary entries to a one-dimensional Array instance at the specified index.
GetEnumerator() Returns an IDictionaryEnumerator that iterates through the ListDictionary.
Remove(Object) Removes the entry with the specified key from the ListDictionary.

ListDictionary Structure (Singly Linked List) "A" ? "Books" "B" ? "Electronics" "C" ? "Smart Wearables" null Head ? First Node ? Second Node ? Third Node ? null Linear search through linked list O(n) time complexity for lookups Best for collections with < 10 items

Using ListDictionary Properties

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      ListDictionary dict1 = new ListDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      
      Console.WriteLine("ListDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      Console.WriteLine("Is the ListDictionary1 having fixed size? = " + dict1.IsFixedSize);
      Console.WriteLine("If ListDictionary1 read-only? = " + dict1.IsReadOnly);
      Console.WriteLine("Is ListDictionary1 synchronized = " + dict1.IsSynchronized);
      Console.WriteLine("The ListDictionary1 has the key M? = " + dict1.Contains("M"));
      
      ListDictionary dict2 = new ListDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      
      Console.WriteLine("\nListDictionary2 key-value pairs...");
      IDictionaryEnumerator demoEnum = dict2.GetEnumerator();
      while (demoEnum.MoveNext())
         Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
      
      Console.WriteLine("Is the ListDictionary2 having fixed size? = " + dict2.IsFixedSize);
      Console.WriteLine("If ListDictionary2 read-only? = " + dict2.IsReadOnly);
      Console.WriteLine("Is ListDictionary2 synchronized = " + dict2.IsSynchronized);
      Console.WriteLine("The ListDictionary2 has the key 5? = " + dict2.Contains("5"));
   }
}

The output of the above code is −

ListDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear
Is the ListDictionary1 having fixed size? = False
If ListDictionary1 read-only? = False
Is ListDictionary1 synchronized = False
The ListDictionary1 has the key M? = False

ListDictionary2 key-value pairs...
Key = 1, Value = One
Key = 2, Value = Two
Key = 3, Value = Three
Key = 4, Value = Four
Key = 5, Value = Five
Key = 6, Value = Six
Is the ListDictionary2 having fixed size? = False
If ListDictionary2 read-only? = False
Is ListDictionary2 synchronized = False
The ListDictionary2 has the key 5? = True

Comparing ListDictionary Objects

Example

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      ListDictionary dict1 = new ListDictionary();
      dict1.Add("A", "Books");
      dict1.Add("B", "Electronics");
      dict1.Add("C", "Smart Wearables");
      dict1.Add("D", "Pet Supplies");
      dict1.Add("E", "Clothing");
      dict1.Add("F", "Footwear");
      
      Console.WriteLine("ListDictionary1 elements...");
      foreach(DictionaryEntry d in dict1) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      ListDictionary dict2 = new ListDictionary();
      dict2.Add("1", "One");
      dict2.Add("2", "Two");
      dict2.Add("3", "Three");
      dict2.Add("4", "Four");
      dict2.Add("5", "Five");
      dict2.Add("6", "Six");
      
      Console.WriteLine("\nListDictionary2 elements...");
      foreach(DictionaryEntry d in dict2) {
         Console.WriteLine(d.Key + " " + d.Value);
      }
      
      ListDictionary dict3 = new ListDictionary();
      dict3 = dict2;
      Console.WriteLine("\nIs ListDictionary3 equal to ListDictionary2? = " + (dict3.Equals(dict2)));
   }
}

The output of the above code is −

ListDictionary1 elements...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

ListDictionary2 elements...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six

Is ListDictionary3 equal to ListDictionary2? = True

Performance Characteristics

Operation Time Complexity Description
Add O(1) Adds to the beginning of the list
Lookup O(n) Linear search through linked list
Remove O(n) Must find the item first, then remove
Contains O(n) Linear search through all keys

Conclusion

ListDictionary is ideal for small collections (under 10 items) due to its simple linked list structure. While it has O(n) lookup time, its low overhead makes it efficient for small datasets. For larger collections, consider using Dictionary<TKey, TValue> or Hashtable which provide O(1) average lookup performance.

Updated on: 2026-03-17T07:04:36+05:30

465 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements