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

 

constructor

 

constructor(aCapacity: Int32)

 

Dictionary<T,U>(Int32 aCapacity)

 

init(_ aCapacity: Int32)

 

Dictionary<T,U>(Int32 aCapacity)

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)

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

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)

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)

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; }

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)

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)

Parameters:

  • Key:

GetKeys  mapped

 

method GetKeys: IEnumerable<T>

 

IEnumerable<T> GetKeys()

 

func GetKeys() -> IEnumerable<T>

 

IEnumerable<T> GetKeys()

GetValues  mapped

 

method GetValues: IEnumerable<U>

 

IEnumerable<U> GetValues()

 

func GetValues() -> IEnumerable<U>

 

IEnumerable<U> GetValues()

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; }

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; }

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)

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)

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)

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; }

 

Count

Gets the number of elements contained in the dictionary.

 

property Count: Int32 read;

 

Int32 Count { get; }

 

var Count: Int32 { get{} }

 

Int32 Count { __get; }

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; }

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; }

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; }

 

constructor

 

constructor(aCapacity: Int32)

 

Dictionary<T,U>(Int32 aCapacity)

 

init(_ aCapacity: Int32)

 

Dictionary<T,U>(Int32 aCapacity)

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)

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

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)

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)

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)

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)

Parameters:

  • Key:

GetKeys  mapped

 

method GetKeys: IEnumerable<T>

 

IEnumerable<T> GetKeys()

 

func GetKeys() -> IEnumerable<T>

 

IEnumerable<T> GetKeys()

GetValues  mapped

 

method GetValues: IEnumerable<U>

 

IEnumerable<U> GetValues()

 

func GetValues() -> IEnumerable<U>

 

IEnumerable<U> GetValues()

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)

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)

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)

Parameters:

  • aKey:
  • aValue: