ListDictionary Class
Represents a collection of key/value pairs that is implemented as an ordered list. This collection is optimized for small collections and performs better than a Hashtable for such cases. Keys are case-sensitive.
Summary
Constructors
-
public ListDictionary() Constructor
Initializes a new instance of the ListDictionary class that is empty, has the default initial capacity, and uses case-sensitive comparisons.
-
public ListDictionary(IEqualityComparer comparer) Constructor
Initializes a new instance of the ListDictionary class that is empty, has the default initial capacity, and uses the specified IEqualityComparer.
- comparer
- The IEqualityComparer to use for comparisons. If null, the default case-sensitive comparer is used.
Properties
-
public int Count { get; } Property
Gets the number of key/value pairs contained in the ListDictionary.
-
public object Values { get; } Property
Gets a collection of the values in the ListDictionary.
-
public object Keys { get; } Property
Gets a collection of the keys in the ListDictionary.
-
public object this[object key] { get; set; } Indexer
Gets or sets the value associated with the specified key.
- key
- The key of the element to get or set.
Methods
-
public void Add(object key, object value) Method
Adds an element with the specified key and value to the ListDictionary.
- key
- The key of the element to add.
- value
- The value of the element to add.
-
public void Remove(object key) Method
Removes the element with the specified key from the ListDictionary.
- key
- The key of the element to remove.
-
public bool Contains(object key) Method
Determines whether the ListDictionary contains an element with the specified key.
- key
- The key to locate in the ListDictionary.
Returns true if the ListDictionary contains an element with the specified key; otherwise, false.
-
public void Clear() Method
Removes all elements from the ListDictionary.
Remarks
The ListDictionary class is suitable for small collections because it iterates through the entire list to perform most operations, such as adding, removing, or searching for an element. For larger collections, consider using a Hashtable or Dictionary<TKey, TValue> for better performance.
The keys in a ListDictionary are case-sensitive by default. This behavior can be modified by providing an IEqualityComparer to the constructor.
Example
C# Example
using System;
using System.Collections.Specialized;
public class Example
{
public static void Main(string[] args)
{
// Create a new ListDictionary
ListDictionary myDictionary = new ListDictionary();
// Add key-value pairs
myDictionary.Add("Key1", "Value1");
myDictionary.Add("Key2", "Value2");
myDictionary.Add("Key3", "Value3");
Console.WriteLine("Number of items: " + myDictionary.Count);
// Access a value by key
Console.WriteLine("Value for Key2: " + myDictionary["Key2"]);
// Check if a key exists
if (myDictionary.Contains("Key1"))
{
Console.WriteLine("Key1 exists in the dictionary.");
}
// Remove an item
myDictionary.Remove("Key2");
Console.WriteLine("Removed Key2.");
Console.WriteLine("Number of items after removal: " + myDictionary.Count);
// Iterate through keys and values
Console.WriteLine("\nAll items:");
foreach (string key in myDictionary.Keys)
{
Console.WriteLine("Key: " + key + ", Value: " + myDictionary[key]);
}
// Clear the dictionary
myDictionary.Clear();
Console.WriteLine("\nCleared the dictionary.");
Console.WriteLine("Number of items after clear: " + myDictionary.Count);
}
}