Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# | ListDictionary Class

  • Difficulty Level : Easy
  • Last Updated : 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.

Constructors

ConstructorDescription
ListDictionary()Creates an empty ListDictionary using the default comparer.
ListDictionary(IComparer)Creates an empty ListDictionary using the specified comparer.

Example:




// C# code to create a ListDictionary
using 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");
  
    // 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);
    }
}
}
Output:
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

Properties

PropertyDescription
CountGets the number of key/value pairs contained in the ListDictionary.
IsFixedSizeGets a value indicating whether the ListDictionary has a fixed size.
IsReadOnlyGets a value indicating whether the ListDictionary is read-only.
IsSynchronizedGets a value indicating whether the ListDictionary is synchronized (thread safe).
Item[Object]Gets or sets the value associated with the specified key.
KeysGets an ICollection containing the keys in the ListDictionary.
SyncRootGets an object that can be used to synchronize access to the ListDictionary.
ValuesGets an ICollection containing the values in the ListDictionary.

Example 1:






// C# code to get the number
// of key/value pairs contained
// in the ListDictionary
using 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);
    }
}
Output:
6

Example 2:




// C# code to check if ListDictionary is read-only
using 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);
    }
}
Output:
False

Methods

MethodDescription
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.
Equals(Object)Determines whether the specified object is equal to the current object.
GetEnumerator()Returns an IDictionaryEnumerator that iterates through the ListDictionary.
GetHashCode()Serves as the default hash function.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
Remove(Object)Removes the entry with the specified key from the ListDictionary.
ToString()Returns a string that represents the current object.

Example 1:




// C# code to add an entry with
// the specified key and value
// into the ListDictionary
using 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");
  
    // 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);
    }
}
}
Output:
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 ListDictionary
using 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("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);
    }
}
}
Output:
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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :