Represents a collection of key/value pairs that are sorted by key. This interface is inherited by the SortedDictionary<TKey, TValue> class.
The IDictionary<TKey, TValue>
interface represents a generic collection of key-value pairs, where each key is unique. It provides methods for adding, removing, and retrieving elements based on their keys.
public interface IDictionary<TKey,TValue> : ICollection<KeyValuePair<TKey,TValue>>, IEnumerable<KeyValuePair<TKey,TValue>>, IEnumerable
Type Parameters:
TKey
: The type of the keys in the dictionary. TKey
must be unique.TValue
: The type of the values in the dictionary.Gets or sets the element with the specified key.
TValue this[TKey key] { get; set; }
key
: The key of the element to get or set.Gets an ICollection that contains the keys of the dictionary.
ICollection<TKey> Keys { get; }
ICollection
that contains the keys of the dictionary.Gets an ICollection that contains the values of the dictionary.
ICollection<TValue> Values { get; }
ICollection
that contains the values of the dictionary.Adds an element to the dictionary with the specified key and value.
void Add(TKey key, TValue value)
key
: The key of the element to add.value
: The value of the element to add.Determines whether the dictionary contains an element with the specified key.
bool ContainsKey(TKey key)
key
: The key to locate in the dictionary.true
if the dictionary contains an element with the specified key; otherwise, false
.Removes the element with the specified key from the dictionary.
bool Remove(TKey key)
key
: The key of the element to remove.true
if the element was successfully removed; otherwise, false
.Gets the value associated with the specified key.
bool TryGetValue(TKey key, out TValue value)
key
: The key of the value to get.value
: When this method returns, contains the value associated with the specified key, if the key is found; otherwise, it contains the default value for the type of the value
parameter.true
if the dictionary contains an element with the specified key; otherwise, false
.Here's a basic example of how to use a dictionary:
using System;
using System.Collections.Generic;
public class Example
{
public static void Main(string[] args)
{
// Create a dictionary
IDictionary<string, int> scores = new Dictionary<string, int>();
// Add elements
scores.Add("Alice", 95);
scores.Add("Bob", 88);
scores.Add("Charlie", 92);
// Access elements by key
Console.WriteLine($"Alice's score: {scores["Alice"]}");
// Check if a key exists
if (scores.ContainsKey("Bob"))
{
Console.WriteLine("Bob is in the dictionary.");
}
// Iterate through the dictionary
Console.WriteLine("\nAll scores:");
foreach (KeyValuePair<string, int> entry in scores)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
// Remove an element
scores.Remove("Bob");
// Try to get a value that might not exist
if (scores.TryGetValue("David", out int davidScore))
{
Console.WriteLine($"David's score: {davidScore}");
}
else
{
Console.WriteLine("David not found in the dictionary.");
}
}
}