Class BaseFlexDictionary<TKey, TValue>
- Namespace
- Garyon.DataStructures
- Assembly
- Garyon.dll
A dictionary that allows indexing or setting values on non-existing keys without throwing exceptions.
public abstract class BaseFlexDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull
Type Parameters
TKeyThe type of the keys that are being added to the dictionary.
TValueThe type of the values that are being paired with the keys in the dictionary.
- Inheritance
-
BaseFlexDictionary<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Derived
-
FlexDictionary<TKey, TValue>
- Inherited Members
- Extension Methods
Constructors
BaseFlexDictionary(BaseFlexDictionary<TKey, TValue>)
public BaseFlexDictionary(BaseFlexDictionary<TKey, TValue> other)
Parameters
otherBaseFlexDictionary<TKey, TValue>
BaseFlexDictionary(IEnumerable<KeyValuePair<TKey, TValue>>)
public BaseFlexDictionary(IEnumerable<KeyValuePair<TKey, TValue>> kvps)
Parameters
kvpsIEnumerable<KeyValuePair<TKey, TValue>>
BaseFlexDictionary(IEnumerable<TKey>, TValue)
public BaseFlexDictionary(IEnumerable<TKey> collection, TValue initialValue)
Parameters
collectionIEnumerable<TKey>initialValueTValue
BaseFlexDictionary(int)
public BaseFlexDictionary(int capacity)
Parameters
capacityint
Fields
Dictionary
The internal Dictionary<TKey, TValue> instance.
protected readonly Dictionary<TKey, TValue> Dictionary
Field Value
- Dictionary<TKey, TValue>
Properties
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
this[TKey]
Gets or sets the value of the specified key. If the key does not exist, it will be added to the dictionary.
public virtual TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key whose value to get or set.
Property Value
- TValue
The value of the key. When getting a non-existent key, it will have
TValue's default value.
Keys
Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.
public ICollection<TKey> Keys { get; }
Property Value
- ICollection<TKey>
An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.
Values
Gets an ICollection<T> containing the values in the IDictionary<TKey, TValue>.
public ICollection<TValue> Values { get; }
Property Value
- ICollection<TValue>
An ICollection<T> containing the values in the object that implements IDictionary<TKey, TValue>.
Methods
Add(KeyValuePair<TKey, TValue>)
Adds an item to the ICollection<T>.
public virtual void Add(KeyValuePair<TKey, TValue> kvp)
Parameters
kvpKeyValuePair<TKey, TValue>
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Add(TKey, TValue)
Adds an element with the provided key and value to the IDictionary<TKey, TValue>.
public virtual void Add(TKey key, TValue value)
Parameters
keyTKeyThe object to use as the key of the element to add.
valueTValueThe object to use as the value of the element to add.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the IDictionary<TKey, TValue>.
- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
AddWithInitializationValue(TKey)
protected void AddWithInitializationValue(TKey key)
Parameters
keyTKey
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Clone()
Clones this FlexDictionary<TKey, TValue> and adds all its keys to the resulting instance.
public abstract BaseFlexDictionary<TKey, TValue> Clone()
Returns
- BaseFlexDictionary<TKey, TValue>
The cloned instance containing the same key-value pairs.
ContainsKey(TKey)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the IDictionary<TKey, TValue>.
Returns
- bool
true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<TKey, TValue>>
An enumerator that can be used to iterate through the collection.
GetNewEntryInitializationValue()
Gets the value that will be stored by default upon creating a new entry through the this accessor. This does not affect the Add(TKey, TValue) function.
protected abstract TValue GetNewEntryInitializationValue()
Returns
- TValue
The new value that will be stored in the new entry.
Remove(TKey)
Removes the element with the specified key from the IDictionary<TKey, TValue>.
public virtual bool Remove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
- bool
true if the element is successfully removed; otherwise, false. This method also returns false if
keywas not found in the original IDictionary<TKey, TValue>.
Exceptions
- ArgumentNullException
keyis null.- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
RemoveKeys(IEnumerable<TKey>)
Removes a collection of keys from this FlexDictionary<TKey, TValue>. Keys that are not present will be simply ignored.
public int RemoveKeys(IEnumerable<TKey> keys)
Parameters
keysIEnumerable<TKey>The keys to remove. Must not be null.
Returns
- int
The number of keys that were removed.
RemoveKeys(params TKey[])
Removes a collection of keys from this FlexDictionary<TKey, TValue>. Keys that are not present will be simply ignored.
public int RemoveKeys(params TKey[] keys)
Parameters
keysTKey[]The keys to remove. Must not be null.
Returns
- int
The number of keys that were removed.
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key whose value to get.
valueTValueWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.