IDictionary Interface
Represents a collection of key/value pairs that are organized by key. Keys must be unique and cannot be null.
Inheritance
IDictionary inherits from ICollection.
IDictionary is implemented by:
- Dictionary<TKey, TValue>
- Hashtable
- SortedList
- and others...
Syntax
public interface IDictionary : ICollection
Members
The IDictionary interface exposes the following members:
Properties
| Name | Description |
|---|---|
IsFixedSize |
Gets a value indicating whether the IDictionary has a fixed size. |
IsReadOnly |
Gets a value indicating whether the IDictionary is read-only. |
Item[Object key] |
Gets or sets the element with the specified key. |
Keys |
Gets an ICollection containing the keys of the IDictionary. |
Values |
Gets an ICollection containing the values of the IDictionary. |
Methods
| Name | Description |
|---|---|
Add(Object key, Object value) |
Adds an element with the specified key and value to the IDictionary. |
Clear() |
Removes all elements from the IDictionary. |
Contains(Object key) |
Determines whether the IDictionary contains an element with the specified key. |
GetEnumerator() |
Returns an enumerator that iterates through the IDictionary. |
Remove(Object key) |
Removes the element with the specified key from the IDictionary. |
Remarks
An IDictionary represents a collection of objects that are accessed through a key.
Each element in the dictionary is a key/value pair. The keys are used to retrieve the corresponding values.
Keys must be unique within the dictionary. Attempting to add an element with a key that already exists
will result in an exception for most implementations.
IDictionary interface uses non-generic Object types for keys and values.
For type-safe collections, consider using the generic IDictionary<TKey, TValue> interface.
IDictionary must ensure that keys are unique and that attempting to add a duplicate key results in an appropriate exception (e.g., ArgumentException).
Examples
Using Hashtable
This example demonstrates how to use the Hashtable class, which implements IDictionary, to store and retrieve data.
using System;
using System.Collections;
public class DictionaryExample
{
public static void Main(string[] args)
{
// Declare and initialize an IDictionary using Hashtable
IDictionary studentAges = new Hashtable();
// Add elements to the dictionary
studentAges.Add("Alice", 20);
studentAges.Add("Bob", 22);
studentAges.Add("Charlie", 21);
// Access elements by key
Console.WriteLine("Alice's age: " + studentAges["Alice"]); // Output: Alice's age: 20
// Check if a key exists
if (studentAges.Contains("Bob"))
{
Console.WriteLine("Bob is in the dictionary."); // Output: Bob is in the dictionary.
}
// Iterate through the dictionary
Console.WriteLine("\nAll students and their ages:");
foreach (DictionaryEntry entry in studentAges)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
// Remove an element
studentAges.Remove("Charlie");
// Get the count of elements
Console.WriteLine($"\nNumber of students remaining: {studentAges.Count}"); // Output: Number of students remaining: 2
}
}