Skip to content
CShift0.32

Dictionary<TKey,

Struct · namespace System

struct Dictionary<TKey, TValue> where TKey : IEquatable<TKey>, IHashable

A 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).

Name Description
Create A new, empty dictionary.
static Dictionary<TKey, TValue> Create()

A new, empty dictionary.

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());.
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.

bool ContainsKey(TKey key)

Whether the dictionary has an entry for key.

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

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).

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.

TValue[] Values()

A new array with the values, in the order of Dictionary<TKey, TValue>.Keys.

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());.