Dictionary<T,U>
Overview
Represents a collection of keys and values.
Every key in a dictionary must be unique. Dictionary does not accepts nil for keys, but allows them for values.
Note: On Nougat platform dictionary internally converts nil
to the NSNull
type.
Use case
You can use Dictionary<T,U> class when you need to store a collection of keys and values:
var Users := new Dictionary<Integer, String>;
Users.Add(1, "First");
Users.Add(2, "Second");
Users.Add(3, "Third");
Users.ForEach(item -> writeLn(Sugar.String.Format("Id: {0} Name: {1}", item.Key, item.Value)));
Output:
Id: 1 Name: First
Id: 2 Name: Second
Id: 3 Name: Third
Loops
Please note: Instead of using for/each or for/in loops in the collections please use ForEach methods.
For example:
var Items := new List<String>(["One", "Two", "Three"]);
Items.ForEach(item -> DoSomething(item));
In the Dictionary<T,U> class ForEach method returns an instance of KeyValue<T,U> class to represent a key/value pair.
Location
- Reference: Sugar
- Namespace: Sugar.Collections
constructor
constructor(aCapacity: Int32)
Dictionary<T,U>(Int32 aCapacity)
init(_ aCapacity: Int32)
Dictionary<T,U>(Int32 aCapacity)
Sub New(aCapacity As Int32)
Parameters:
- aCapacity:
Add mapped
Adds specified key and value to the dictionary.
method Add(Key: T; Value: U)
void Add(T Key, U Value)
func Add(_ Key: T, _ Value: U)
void Add(T Key, U Value)
Sub Add(Key As T, Value As U)
Parameters:
- Key: The key of the element to add.
- Value: The value of the element to add.
Clear mapped
Removes all elements from the dictionary.
method Clear
void Clear()
func Clear()
void Clear()
Sub Clear()
ContainsKey mapped
Determines whether the dictionary contains specified key.
method ContainsKey(Key: T): Boolean
Boolean ContainsKey(T Key)
func ContainsKey(_ Key: T) -> Boolean
Boolean ContainsKey(T Key)
Function ContainsKey(Key As T) As Boolean
Parameters:
- Key: Determines whether the dictionary contains specified key.
ContainsValue mapped
Determines whether the dictionary contains specified value.
method ContainsValue(Value: U): Boolean
Boolean ContainsValue(U Value)
func ContainsValue(_ Value: U) -> Boolean
Boolean ContainsValue(U Value)
Function ContainsValue(Value As U) As Boolean
Parameters:
- Value: The value to locate in the dictionary.
Count
Gets the number of elements contained in the dictionary.
property Count: Int32 read;
Int32 Count { get; }
var Count: Int32 { get{} }
Int32 Count { __get; }
ReadOnly Property Count() As Int32
ForEach mapped
Performs the specified action on each element of the dictionary. Use this method inststead of for/in or for/each loops.
method ForEach(Action: Action<KeyValuePair<T,U>>)
void ForEach(Action<KeyValuePair<T,U>> Action)
func ForEach(_ Action: Action<KeyValuePair<T,U>>)
void ForEach(Action<KeyValuePair<T,U>> Action)
Sub ForEach(Action As Action<KeyValuePair<T,U>>)
Parameters:
- Action: The action to perform on each element.
GetItem mapped
method GetItem(Key: T): U
U GetItem(T Key)
func GetItem(_ Key: T) -> U
U GetItem(T Key)
Function GetItem(Key As T) As U
Parameters:
- Key:
GetKeys mapped
method GetKeys: IEnumerable<T>
IEnumerable<T> GetKeys()
func GetKeys() -> IEnumerable<T>
IEnumerable<T> GetKeys()
Function GetKeys() As IEnumerable<T>
GetValues mapped
method GetValues: IEnumerable<U>
IEnumerable<U> GetValues()
func GetValues() -> IEnumerable<U>
IEnumerable<U> GetValues()
Function GetValues() As IEnumerable<U>
Item
Gets or sets a value of the specified key.
property Item[Key: T]: U read write;
U Item[T Key] { get; set; }
subscript Item(_ Key: T) -> U { get{} set{} }
U Item[T Key] { __get; __set; }
Property Item(Key As T) As U
Keys
Gets an array which contains all keys.
property Keys: IEnumerable<T> read;
IEnumerable<T> Keys { get; }
var Keys: IEnumerable<T> { get{} }
IEnumerable<T> Keys { __get; }
ReadOnly Property Keys() As IEnumerable<T>
Remove mapped
Removes first occurrence of the specified key.
method Remove(Key: T): Boolean
Boolean Remove(T Key)
func Remove(_ Key: T) -> Boolean
Boolean Remove(T Key)
Function Remove(Key As T) As Boolean
Parameters:
- Key: The key to remove from the dictionary.
SetItem mapped
method SetItem(Key: T; Value: U)
void SetItem(T Key, U Value)
func SetItem(_ Key: T, _ Value: U)
void SetItem(T Key, U Value)
Sub SetItem(Key As T, Value As U)
Parameters:
- Key:
- Value:
TryGetValue mapped
method TryGetValue(aKey: T; out aValue: U): Boolean
Boolean TryGetValue(T aKey, out U aValue)
func TryGetValue(_ aKey: T, _ aValue: U) -> Boolean
Boolean TryGetValue(T aKey, __out U aValue)
Function TryGetValue(aKey As T, <OutAttribute> ByRef aValue As U) As Boolean
Parameters:
- aKey:
- aValue:
Values
Gets an array which contains all values.
property Values: IEnumerable<U> read;
IEnumerable<U> Values { get; }
var Values: IEnumerable<U> { get{} }
IEnumerable<U> Values { __get; }
ReadOnly Property Values() As IEnumerable<U>
Count
Gets the number of elements contained in the dictionary.
property Count: Int32 read;
Int32 Count { get; }
var Count: Int32 { get{} }
Int32 Count { __get; }
ReadOnly Property Count() As Int32
Item
Gets or sets a value of the specified key.
property Item[Key: T]: U read write;
U Item[T Key] { get; set; }
subscript Item(_ Key: T) -> U { get{} set{} }
U Item[T Key] { __get; __set; }
Property Item(Key As T) As U
Keys
Gets an array which contains all keys.
property Keys: IEnumerable<T> read;
IEnumerable<T> Keys { get; }
var Keys: IEnumerable<T> { get{} }
IEnumerable<T> Keys { __get; }
ReadOnly Property Keys() As IEnumerable<T>
Values
Gets an array which contains all values.
property Values: IEnumerable<U> read;
IEnumerable<U> Values { get; }
var Values: IEnumerable<U> { get{} }
IEnumerable<U> Values { __get; }
ReadOnly Property Values() As IEnumerable<U>
constructor
constructor(aCapacity: Int32)
Dictionary<T,U>(Int32 aCapacity)
init(_ aCapacity: Int32)
Dictionary<T,U>(Int32 aCapacity)
Sub New(aCapacity As Int32)
Parameters:
- aCapacity:
Add mapped
Adds specified key and value to the dictionary.
method Add(Key: T; Value: U)
void Add(T Key, U Value)
func Add(_ Key: T, _ Value: U)
void Add(T Key, U Value)
Sub Add(Key As T, Value As U)
Parameters:
- Key: The key of the element to add.
- Value: The value of the element to add.
Clear mapped
Removes all elements from the dictionary.
method Clear
void Clear()
func Clear()
void Clear()
Sub Clear()
ContainsKey mapped
Determines whether the dictionary contains specified key.
method ContainsKey(Key: T): Boolean
Boolean ContainsKey(T Key)
func ContainsKey(_ Key: T) -> Boolean
Boolean ContainsKey(T Key)
Function ContainsKey(Key As T) As Boolean
Parameters:
- Key: Determines whether the dictionary contains specified key.
ContainsValue mapped
Determines whether the dictionary contains specified value.
method ContainsValue(Value: U): Boolean
Boolean ContainsValue(U Value)
func ContainsValue(_ Value: U) -> Boolean
Boolean ContainsValue(U Value)
Function ContainsValue(Value As U) As Boolean
Parameters:
- Value: The value to locate in the dictionary.
ForEach mapped
Performs the specified action on each element of the dictionary. Use this method inststead of for/in or for/each loops.
method ForEach(Action: Action<KeyValuePair<T,U>>)
void ForEach(Action<KeyValuePair<T,U>> Action)
func ForEach(_ Action: Action<KeyValuePair<T,U>>)
void ForEach(Action<KeyValuePair<T,U>> Action)
Sub ForEach(Action As Action<KeyValuePair<T,U>>)
Parameters:
- Action: The action to perform on each element.
GetItem mapped
method GetItem(Key: T): U
U GetItem(T Key)
func GetItem(_ Key: T) -> U
U GetItem(T Key)
Function GetItem(Key As T) As U
Parameters:
- Key:
GetKeys mapped
method GetKeys: IEnumerable<T>
IEnumerable<T> GetKeys()
func GetKeys() -> IEnumerable<T>
IEnumerable<T> GetKeys()
Function GetKeys() As IEnumerable<T>
GetValues mapped
method GetValues: IEnumerable<U>
IEnumerable<U> GetValues()
func GetValues() -> IEnumerable<U>
IEnumerable<U> GetValues()
Function GetValues() As IEnumerable<U>
Remove mapped
Removes first occurrence of the specified key.
method Remove(Key: T): Boolean
Boolean Remove(T Key)
func Remove(_ Key: T) -> Boolean
Boolean Remove(T Key)
Function Remove(Key As T) As Boolean
Parameters:
- Key: The key to remove from the dictionary.
SetItem mapped
method SetItem(Key: T; Value: U)
void SetItem(T Key, U Value)
func SetItem(_ Key: T, _ Value: U)
void SetItem(T Key, U Value)
Sub SetItem(Key As T, Value As U)
Parameters:
- Key:
- Value:
TryGetValue mapped
method TryGetValue(aKey: T; out aValue: U): Boolean
Boolean TryGetValue(T aKey, out U aValue)
func TryGetValue(_ aKey: T, _ aValue: U) -> Boolean
Boolean TryGetValue(T aKey, __out U aValue)
Function TryGetValue(aKey As T, <OutAttribute> ByRef aValue As U) As Boolean
Parameters:
- aKey:
- aValue: