Dictionary<TKey,
Struct · namespace System
struct Dictionary<TKey, TValue> where TKey : IEquatable<TKey>, IHashableA hash table (separate chaining). Keys keep their insertion order in Keys(), Values() and Entries() as long as no entry was removed.
using System;
var ages = Dictionary<string, int>.Create();ages.Set("Ann", 31);if (ages.TryGet("Ann") is int age) Console.WriteLine(age);foreach (var key in ages.Keys()) Console.WriteLine(key);TKey must be IEquatable and IHashable: numbers, bool, char, enums and string are, and your own structs are as soon as they define ‘bool Equals(T other)’ and ‘int GetHashCode()’.
Like List<T>, a Dictionary is a handle to shared storage (see List), made by Dictionary<K, V>.Create(). The zero
value (new(), a field without a value) is an empty dictionary that can be read but not changed: Set panics.
dict[key] reads a value (Dictionary<TKey, TValue>.Get), dict[key] = value sets one
(Dictionary<TKey, TValue>.Set).
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Create |
A new, empty dictionary. |
Create
Section titled “Create”static Dictionary<TKey, TValue> Create()A new, empty dictionary.
Methods
Section titled “Methods”| Name | Description |
|---|---|
IsCreated |
Whether the dictionary has storage: made by Create(), not the zero value (which cannot change). |
Count |
The number of entries. |
ContainsKey |
Whether the dictionary has an entry for key. |
TryGet |
The value of key, if there is one: if (ages.TryGet("Ann") is int age) .... |
Get |
The value of key (also dict[key]). |
GetOrDefault |
The value of key, or fallback if the key is not present. |
Set |
Adds an entry, or replaces the value of an existing key (also dict[key] = value). |
Add |
Adds an entry for a new key. |
Remove |
Removes the entry of key. |
Clear |
Removes all entries. |
Keys |
A new array with the keys, in insertion order as long as no entry was removed. |
Values |
A new array with the values, in the order of Dictionary<TKey, TValue>.Keys. |
Entries |
A new array with the entries (key and value), in the order of Dictionary<TKey, TValue>.Keys: foreach (var e in ages.Entries()) Console.WriteLine(e.Key + " " + e.Value.ToString());. |
IsCreated
Section titled “IsCreated”bool IsCreated()Whether the dictionary has storage: made by Create(), not the zero value (which cannot change).
Since 0.26
int Count()The number of entries.
ContainsKey
Section titled “ContainsKey”bool ContainsKey(TKey key)Whether the dictionary has an entry for key.
TryGet
Section titled “TryGet”Optional<TValue> TryGet(TKey key)The value of key, if there is one: if (ages.TryGet("Ann") is int age) ....
Returns nothing (null) if the key is not present.
TValue Get(TKey key)The value of key (also dict[key]). Dictionary<TKey, TValue>.TryGet asks without panicking.
Panics when the key is not present.
Since 0.04
GetOrDefault
Section titled “GetOrDefault”TValue GetOrDefault(TKey key, TValue fallback)The value of key, or fallback if the key is not present.
void Set(TKey key, TValue value)Adds an entry, or replaces the value of an existing key (also dict[key] = value).
Panics when the dictionary was not created (Dictionary<TKey, TValue>.IsCreated).
Error<void> Add(TKey key, TValue value)Adds an entry for a new key.
Returns an error if the key exists already (the dictionary is not changed).
Remove
Section titled “Remove”bool Remove(TKey key)Removes the entry of key.
Returns whether there was an entry.
void Clear()Removes all entries.
TKey[] Keys()A new array with the keys, in insertion order as long as no entry was removed.
Values
Section titled “Values”TValue[] Values()A new array with the values, in the order of Dictionary<TKey, TValue>.Keys.
Entries
Section titled “Entries”KeyValuePair<TKey, TValue>[] Entries()A new array with the entries (key and value), in the order of Dictionary<TKey, TValue>.Keys:
foreach (var e in ages.Entries()) Console.WriteLine(e.Key + " " + e.Value.ToString());.