Table of Contents

Class Dictionary<TKey, TValue>

Namespace
Godot.Collections
Assembly
GodotSharp.dll

Typed wrapper around Godot's Dictionary class, a dictionary of Variant typed elements allocated in the engine in C++. Useful when interfacing with the engine. Otherwise prefer .NET collections such as Dictionary<TKey, TValue>.

public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

Type Parameters

TKey

The type of the dictionary's keys.

TValue

The type of the dictionary's values.

Inheritance
Dictionary<TKey, TValue>
Implements
IDictionary<TKey, TValue>
ICollection<KeyValuePair<TKey, TValue>>
IReadOnlyDictionary<TKey, TValue>
IEnumerable<KeyValuePair<TKey, TValue>>
Inherited Members

Constructors

Dictionary()

Constructs a new empty Dictionary<TKey, TValue>.

public Dictionary()

Dictionary(Dictionary)

Constructs a new Dictionary<TKey, TValue> from the given dictionary's elements.

public Dictionary(Dictionary dictionary)

Parameters

dictionary Dictionary

The dictionary to construct from.

Exceptions

ArgumentNullException

The dictionary is null.

Dictionary(IDictionary<TKey, TValue>)

Constructs a new Dictionary<TKey, TValue> from the given dictionary's elements.

public Dictionary(IDictionary<TKey, TValue> dictionary)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to construct from.

Exceptions

ArgumentNullException

The dictionary is null.

Properties

Count

Returns the number of elements in this Dictionary<TKey, TValue>. This is also known as the size or length of the dictionary.

public int Count { get; }

Property Value

int

The number of elements.

IsReadOnly

Returns true if the dictionary is read-only. See MakeReadOnly().

public bool IsReadOnly { get; }

Property Value

bool

this[TKey]

Returns the value at the given key.

public TValue this[TKey key] { get; set; }

Parameters

key TKey

Property Value

TValue

The value at the given key.

Exceptions

InvalidOperationException

The property is assigned and the dictionary is read-only.

KeyNotFoundException

The property is retrieved and an entry for key does not exist in the dictionary.

Keys

Gets the collection of keys in this Dictionary<TKey, TValue>.

public ICollection<TKey> Keys { get; }

Property Value

ICollection<TKey>

Values

Gets the collection of elements in this Dictionary<TKey, TValue>.

public ICollection<TValue> Values { get; }

Property Value

ICollection<TValue>

Methods

Add(TKey, TValue)

Adds an object value at key key to this Dictionary<TKey, TValue>.

public void Add(TKey key, TValue value)

Parameters

key TKey

The key at which to add the object.

value TValue

The object to add.

Exceptions

InvalidOperationException

The dictionary is read-only.

ArgumentException

An element with the same key already exists.

Clear()

Clears the dictionary, removing all entries from it.

public void Clear()

Exceptions

InvalidOperationException

The dictionary is read-only.

ContainsKey(TKey)

Checks if this Dictionary<TKey, TValue> contains the given key.

public bool ContainsKey(TKey key)

Parameters

key TKey

The key to look for.

Returns

bool

Whether or not this dictionary contains the given key.

Duplicate(bool)

Returns a copy of the Dictionary<TKey, TValue>. If deep is true, a deep copy is performed: all nested arrays and dictionaries are duplicated and will not be shared with the original dictionary. If false, a shallow copy is made and references to the original nested arrays and dictionaries are kept, so that modifying a sub-array or dictionary in the copy will also impact those referenced in the source dictionary. Note that any GodotObject derived elements will be shallow copied regardless of the deep setting.

public Dictionary<TKey, TValue> Duplicate(bool deep = false)

Parameters

deep bool

If true, performs a deep copy.

Returns

Dictionary<TKey, TValue>

A new Godot Dictionary.

GetEnumerator()

Gets an enumerator for this Dictionary<TKey, TValue>.

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

Returns

IEnumerator<KeyValuePair<TKey, TValue>>

An enumerator.

MakeReadOnly()

Makes the Dictionary<TKey, TValue> read-only, i.e. disabled modying of the dictionary's elements. Does not apply to nested content, e.g. content of nested dictionaries.

public void MakeReadOnly()

Merge(Dictionary<TKey, TValue>, bool)

Adds entries from dictionary to this dictionary. By default, duplicate keys are not copied over, unless overwrite is true.

public void Merge(Dictionary<TKey, TValue> dictionary, bool overwrite = false)

Parameters

dictionary Dictionary<TKey, TValue>

Dictionary to copy entries from.

overwrite bool

If duplicate keys should be copied over as well.

Exceptions

InvalidOperationException

The dictionary is read-only.

RecursiveEqual(Dictionary<TKey, TValue>)

Compares this Dictionary<TKey, TValue> against the otherDictionary<TKey, TValue> recursively. Returns true if the two dictionaries contain the same keys and values. The order of the entries does not matter. otherwise.

public bool RecursiveEqual(Dictionary<TKey, TValue> other)

Parameters

other Dictionary<TKey, TValue>

The other dictionary to compare against.

Returns

bool

true if the dictionaries contain the same keys and values, false otherwise.

Remove(TKey)

Removes an element from this Dictionary<TKey, TValue> by key.

public bool Remove(TKey key)

Parameters

key TKey

The key of the element to remove.

Returns

bool

Exceptions

InvalidOperationException

The dictionary is read-only.

ToString()

Converts this Dictionary<TKey, TValue> to a string.

public override string ToString()

Returns

string

A string representation of this dictionary.

TryGetValue(TKey, out TValue)

Gets the value for the given key in the dictionary. Returns true if an entry for the given key exists in the dictionary; otherwise, returns false.

public bool TryGetValue(TKey key, out TValue value)

Parameters

key TKey

The key of the element to get.

value TValue

The value at the given key.

Returns

bool

If an entry was found for the given key.

Operators

explicit operator Dictionary(Dictionary<TKey, TValue>)

Converts this typed Dictionary<TKey, TValue> to an untyped Dictionary.

public static explicit operator Dictionary(Dictionary<TKey, TValue> from)

Parameters

from Dictionary<TKey, TValue>

The typed dictionary to convert.

Returns

Dictionary

A new Godot Dictionary, or null if was null.

explicit operator Dictionary<TKey, TValue>(Variant)

public static explicit operator Dictionary<TKey, TValue>(Variant from)

Parameters

from Variant

Returns

Dictionary<TKey, TValue>

implicit operator Variant(Dictionary<TKey, TValue>)

public static implicit operator Variant(Dictionary<TKey, TValue> from)

Parameters

from Dictionary<TKey, TValue>

Returns

Variant