Dictionary
- Last UpdatedMar 12, 2025
- 2 minute read
This topic is about the Dictionary data type.
Value syntax
Dictionary<KeyType,valueType> d = { {key1,value1},{key2,value2} }
Syntax notes
Previous assignment syntax can be used only during initialization. The following code will raise an error.
Dictionary<KeyType,valueType> d;
d = { {key1,value1},{key2,value2} };
After an empty Dictionary is declared, it can be populated either by using the implemented API listed below or by assigning it the keys and values from another Dictionary.
Dictionary<KeyType,valueType> d;
Dictionary<KeyType,valueType> d2 = { {key1,value1},{key2,value2} };
d = d2;
Description
Dictionary type represents a collection of key and value pairs.
-
The default value is an empty dictionary of the specified keyType and valueType.
-
Dictionary type partially supports Comparison operators.
-
In addition, it supports Indexer operator [].
Properties
|
Method |
Return type |
Parameters |
Description |
|---|---|---|---|
|
Count |
int |
|
Used to obtain the number of key/value pairs contained in the dictionary. |
|
Keys |
List |
|
Gets a List containing the keys in the Dictionary<KeyType,valueType>. |
|
Values |
List |
|
Gets a List containing the values in the Dictionary<KeyType,valueType>. |
Methods
|
Method |
Return type |
Parameters |
Description |
|---|---|---|---|
|
Clear |
|
|
Removes all elements from the dictionary. |
|
Clone |
Dictionary<keyType valueType> |
Returns a cloned copy of the dictionary. Please observe that if the dictionary contains reference type entries these are not copied but referenced in the returned entry. |
|
|
ContainsKey |
bool |
KeyType key |
Returns true if the dictionary contains the given key, including character case. |
|
Set |
|
KeyType key, ValueType value |
Sets to value the value associated with the specified key. |
|
Remove |
bool |
KeyType key |
Removes the value with the specified key from the Dictionary<KeyType,valueType>. It returns true if the object is successfully removed and false if key is not found in the Dictionary<KeyType,valueType>. |
|
Union |
Dictionary<keyType, valueType> |
Dictionary<keyType, valueType> |
Produces the set union of two dictionaries that have the same keyType and valueType. |