Class FixedKeyDictionary<TKey, TValue>
- Namespace
- Gamelogic.Extensions
- Assembly
- Gamelogic.Extensions.dll
Represents a dictionary with a fixed set of keys.
[Version(4, 5, 0)]
[Serializable]
public class FixedKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
TKeyTValue
- Inheritance
-
FixedKeyDictionary<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IReadOnlyDictionary<TKey, TValue>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Inherited Members
- Extension Methods
Remarks
This is not meant to be used for large dictionaries, as it does not provide constant time access to elements.
A list is used to store elements so that they are more easily serialized (since Unity 2020.1).
However, it is not possible for a key to appear twice.
Furthermore, the usual way to use this is to map all IDs to GameObjects; this is done by calling Validate(IEnumerable<TKey>) with a list of all IDs in the order they should appear. This will ensure that all IDs are in the dictionary, and that Keys (and their corresponding Values) are in the correct order.
It is possible for values to be null.
Null is not allowed as a key.
We would have liked to set the keys in the constructor, but since we are building this class to work with Unity's serialization, a method that sets the keys is used instead (Validate(IEnumerable<TKey>)). This can be called from OnValidate in the component where it is being used. This means setting the keys happens at edit time, not runtime.
During development, keys may change; calling Clear() (which appears as an inspector button) will reset IDs.
Fields
Properties
- Count
Gets the number of key–value pairs contained in the dictionary.
- IsReadOnly
Gets a value indicating whether the dictionary is read-only.
- this[TKey]
Gets or sets the value associated with the specified key.
- Keys
Gets a collection containing the keys of the dictionary.
- Values
Gets a collection containing the values of the dictionary.
Methods
- Add(KeyValuePair<TKey, TValue>)
Adds the specified key–value pair to the dictionary.
- Add(TKey, TValue)
Adds a key–value pair to the dictionary.
- Clear()
Removes all key–value pairs from the dictionary.
- Contains(KeyValuePair<TKey, TValue>)
Determines whether the dictionary contains the specified key–value pair.
- ContainsKey(TKey)
Determines whether the dictionary contains the specified key.
- CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the dictionary to an array, starting at the specified index.
- GetEnumerator()
Returns an enumerator that iterates through the dictionary.
- Remove(KeyValuePair<TKey, TValue>)
Removes the specified key–value pair.
- Remove(TKey)
Removes the entry with the specified key.
- TryGetValue(TKey, out TValue)
Attempts to retrieve the value associated with the specified key.
- Validate(IEnumerable<TKey>)
Validates the dictionary by ensuring that all provided keys exist and appear in the specified order. Missing keys are added with default values, and existing keys are re-ordered.