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.
- It uses a single LinkedList to store key-value pairs.
- It preserves insertion order.
- The key cannot be null, but values can be null.
- It is not suitable for large datasets due to performance limitations.
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}");
}
}
}
OutputElements in the ListDictionary:
Geek1: 1
Geek2: 2
Geek3: 3
Declarartion of ListDictionary
In C#, the declaration of Dictionary can be done as:
ListDictionary variableName = new ListDictionary();
Constructors
Constructors | Description |
---|
ListDictionary() | Creates an empty ListDictionary using the default comparer. |
ListDictionary(IComparer) | Creates an empty ListDictionary using the specified compare |
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);
}
}
}
OutputTotal 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: 4
Properties
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);
}
}
OutputTotal 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
Similar Reads
C# Dictionary Class In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value.In Dictionary, each entry consists of a key and its associated value.It provides constant time
7 min read
C# StringDictionary Class In C#, the StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys.Keys are always strings, and
5 min read
C# SortedDictionary Class In C#, the SortedDictionary<TKey,TValue> class represents the collection of key/value pairs. This pair is in sorted form and the sorting is done on the key. This class is defined under System.Collections.Generic namespace. In the SortedDictionary class, the keys are immutable, always unique, a
5 min read
C# HybridDictionary Class In C#, the HybridDictionary Class is the part of the System.Collections.Specialized namespace. It is a collection that combines the features of both a Hashtable and a ListDictionary. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when t
5 min read
C# OrderedDictionary Class In C#, the OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized namespace.It implements both IDicitonary and ICollection interfaces.Allows indexed access to elements via both keys and numeric index
6 min read