A Dictionary template is an ordered hashmap implementation.
The Belte public interface for the Dictionary template can be found here.
The Dictionary template has two template parameters. The first corresponds to the key type, the second corresponds to the value type.
| Signature | Description |
|-|-|
| new Dictionary<type TKey, type TValue>() | Creates an empty dictionary. |
| Dictionary<type TKey, type TValue>(int!) | Creates an empty dictionary with a starting capacity. |
| new Dictionary<type TKey, type TValue>(EqualityComparer
For example, to create an empty dictionary where the key type is int and the
value type is string:
new Dictionary<int, string>();
| Signature | Description |
|---|---|
void Add(TKey, TValue) |
Adds a key value pair. |
void Clear() |
Removes all elements. |
bool! ContainsKey(TKey) |
If the dictionary contains the given key. |
bool! ContainsValue(TValue) |
If the dictionary contains the given value. |
int! Length() |
The number of elements. |
bool Remove(TKey) |
Removes the pair with the given key. Returns true if succeeded, or false if the key was not present. |
| Signature | Description |
|---|---|
static ref TValue operator[](Dictionary<TKey, TValue>, TKey) |
Gets the value associated with the given key. |
For example, to set and get a value from a dictionary:
var myDict = new Dictionary<int, string>();
myDict.Add(3, "test");
myDict[3] = "new string";
var myValue = myDict[3];
Note that the indexing operator does not create new entries in the dictionary, only modifies them.
Initializer dictionaries are special syntax that create dictionaries implicitly.
var myDict = { "A": 1, "B": 2, "C": 3 };
The above is shorthand for:
Dictionary<string, int> myDict = new Dictionary<string, int>();
myDict.Add("A", 1);
myDict.Add("B", 2);
myDict.Add("C", 3);