Last Updated : 11 Jul, 2025
In C#, the ListDictionary class is the part of the System.Collections.Specialized namespace. It is a collection that stores key-value pairs. It is used in scenarios where the number of elements is relatively small and the order of insertion needs to be preserved.
Example: This example demonstrates how to add elements to a ListDictionary and then display its elements.
C#
// C# program to add elements to a ListDictionary
using System;
using System.Collections.Specialized;
using System.Collections;
class Geeks
{
static void Main()
{
// Create a new ListDictionary
ListDictionary ld = new ListDictionary();
// Add key-value pairs to the ListDictionary
ld.Add("Geek1", 1);
ld.Add("Geek2", 2);
ld.Add("Geek3", 3);
// Display the elements in the ListDictionary
Console.WriteLine("Elements in the ListDictionary:");
foreach (DictionaryEntry i in ld)
{
Console.WriteLine($"{i.Key}: {i.Value}");
}
}
}
Elements in the ListDictionary: Geek1: 1 Geek2: 2 Geek3: 3Declarartion of ListDictionary
In C#, the declaration of Dictionary can be done as:
Constructors Constructors Description ListDictionary() Creates an empty ListDictionary using the default comparer. ListDictionary(IComparer) Creates an empty ListDictionary using the specified compareListDictionary variableName = new ListDictionary();
Example: This example demonstrates how to retrieve the count of pairs and displaying the key-value pair.
C#
// C# program to demonstrates the
// working of Count property
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a ListDictionary named ld
ListDictionary ld = new ListDictionary();
// Adding key/value pairs
ld.Add("Java", "1");
ld.Add("C++", "2");
ld.Add("Js", "3");
ld.Add("Python", "4");
// To get count of key-value pairs
Console.WriteLine("Total number of Key-value pairs: "
+ ld.Count);
// Displaying the key-value pairs
Console.WriteLine("Displaying the key-value pair: ");
foreach(DictionaryEntry i in ld)
{
Console.WriteLine("Key: "+i.Key + " , "+ "Value: "
+ i.Value);
}
}
}
Total number of Key-value pairs: 4 Displaying the key-value pair: Key: Java , Value: 1 Key: C++ , Value: 2 Key: Js , Value: 3 Key: Python , Value: 4Properties Properties 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. SyncRoot Gets an object that can be used to synchronize access to the ListDictionary. Values Gets an ICollection containing the values in the ListDictionary.
Example 1: This example demonstrates how to retrieve the count of pairs.
C#
// C# Program to get the number
// of key/value pairs contained
// in the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a ListDictionary named ld
ListDictionary ld = new ListDictionary();
// Adding key/value pairs
ld.Add("100", "1");
ld.Add("200", "2");
ld.Add("300", "3");
ld.Add("400", "4");
// Displaying the number of key/value
// pairs contained in the ListDictionary
Console.WriteLine(ld.Count);
}
}
Example 2: This example checks if the dictionary is read-only.
C#
// C# program demonstrates the
// use of IsReadOnly properity
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main() {
// Creating a ListDictionary named ld
ListDictionary ld = new ListDictionary();
// Adding key/value pairs in l
ld.Add("100", "1");
ld.Add("200", "2");
ld.Add("300", "3");
ld.Add("400", "4");
// Checking if ListDictionary is read-only
Console.WriteLine(ld.IsReadOnly);
}
}
Methods Methods 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. 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: This example demonstrates how to remove all the entries using Clear() method.
C#
// C# Program to remove all entries
// from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a ListDictionary named ld
ListDictionary ld = new ListDictionary();
// Adding key/value pairs in l
ld.Add("I", "first");
ld.Add("II", "second");
// To get count of key/value pairs
Console.WriteLine("Total key-value pairs in ListDictionary are: "
+ ld.Count);
// Displaying the key/value pairs
Console.WriteLine("The key-value pairs in ListDictionary are: ");
foreach(DictionaryEntry i in ld)
{
Console.WriteLine(i.Key + " " + i.Value);
}
// Removing all entries from the ListDictionary
ld.Clear();
// To get count of key/value pairs
Console.WriteLine("Total key/value pairs in ListDictionary are: "
+ ld.Count);
}
}
Total key-value pairs in ListDictionary are: 2 The key-value pairs in ListDictionary are: I first II second Total key/value pairs in ListDictionary are: 0
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4