Class KeyedCollection<TKey, TValue>
- Namespace
- Ecng.Collections
- Assembly
- Ecng.Collections.dll
Represents an abstract dictionary-backed collection that manages key-value pairs with customizable behavior.
public abstract class KeyedCollection<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
TKey
The type of keys in the collection.
TValue
The type of values in the collection.
- Inheritance
-
KeyedCollection<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Derived
- Inherited Members
- Extension Methods
Constructors
KeyedCollection()
Initializes a new instance of the KeyedCollection<TKey, TValue> class with a default dictionary.
protected KeyedCollection()
KeyedCollection(IDictionary<TKey, TValue>)
Represents an abstract dictionary-backed collection that manages key-value pairs with customizable behavior.
protected KeyedCollection(IDictionary<TKey, TValue> innerDictionary)
Parameters
innerDictionary
IDictionary<TKey, TValue>
KeyedCollection(IEqualityComparer<TKey>)
Initializes a new instance of the KeyedCollection<TKey, TValue> class with a specified key comparer.
protected KeyedCollection(IEqualityComparer<TKey> comparer)
Parameters
comparer
IEqualityComparer<TKey>The equality comparer for keys, or null to use the default comparer.
Properties
Count
Gets the number of key-value pairs in the collection.
public virtual int Count { get; }
Property Value
InnerDictionary
Gets the underlying dictionary that stores the key-value pairs.
protected IDictionary<TKey, TValue> InnerDictionary { get; }
Property Value
- IDictionary<TKey, TValue>
this[TKey]
Gets or sets the value associated with the specified key.
public virtual TValue this[TKey key] { get; set; }
Parameters
key
TKeyThe key whose value is to be retrieved or set.
Property Value
- TValue
The value associated with the specified key.
Exceptions
- KeyNotFoundException
Thrown when getting a value and the key is not found in the collection.
- ArgumentNullException
Thrown when setting a value and
key
is null, if the underlying dictionary does not permit null keys.
Keys
Gets a collection containing the keys in the dictionary.
public ICollection<TKey> Keys { get; }
Property Value
- ICollection<TKey>
Values
Gets a collection containing the values in the dictionary.
public ICollection<TValue> Values { get; }
Property Value
- ICollection<TValue>
Methods
Add(KeyValuePair<TKey, TValue>)
Adds a key-value pair to the collection.
public void Add(KeyValuePair<TKey, TValue> item)
Parameters
item
KeyValuePair<TKey, TValue>The key-value pair to add.
Exceptions
- ArgumentException
Thrown when the pair cannot be added, as determined by CanAdd(TKey, TValue).
Add(TKey, TValue)
Adds the specified key-value pair to the collection.
public virtual void Add(TKey key, TValue value)
Parameters
key
TKeyThe key to add.
value
TValueThe value to add.
Exceptions
- ArgumentException
Thrown when the key-value pair cannot be added, as determined by CanAdd(TKey, TValue).
- ArgumentNullException
Thrown when
key
is null and the underlying dictionary does not permit null keys.
CanAdd(TKey, TValue)
Determines whether a key-value pair can be added to the collection.
protected virtual bool CanAdd(TKey key, TValue value)
Parameters
key
TKeyThe key to check.
value
TValueThe value to check.
Returns
- bool
True if the pair can be added; otherwise, false.
Remarks
This method is intended for derived classes to enforce custom addition constraints. The default implementation always returns true.
Clear()
Removes all items from the collection.
public virtual void Clear()
Contains(KeyValuePair<TKey, TValue>)
Determines whether the collection contains a specific key-value pair.
public bool Contains(KeyValuePair<TKey, TValue> item)
Parameters
item
KeyValuePair<TKey, TValue>The key-value pair to locate.
Returns
- bool
True if the pair is found; otherwise, false.
Remarks
This implementation only checks the presence of the key, not the value equality.
ContainsKey(TKey)
Determines whether the collection contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
key
TKeyThe key to locate.
Returns
- bool
True if the key is found; otherwise, false.
CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the collection to an array, starting at the specified index.
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
Parameters
array
KeyValuePair<TKey, TValue>[]The destination array to copy to.
arrayIndex
intThe zero-based index in the array at which copying begins.
Exceptions
- ArgumentNullException
Thrown when
array
is null.- ArgumentOutOfRangeException
Thrown when
arrayIndex
is negative.- ArgumentException
Thrown when the destination array is too small to accommodate the elements.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public virtual IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<TKey, TValue>>
An IEnumerator<T> that can be used to iterate through the collection.
OnAdded(TKey, TValue)
Called after a key-value pair has been added to the collection.
protected virtual void OnAdded(TKey key, TValue value)
Parameters
key
TKeyThe key that was added.
value
TValueThe value that was added.
Remarks
This method is intended for derived classes to implement custom logic after an addition occurs.
OnAdding(TKey, TValue)
Called before adding a key-value pair to the collection.
protected virtual void OnAdding(TKey key, TValue value)
Parameters
key
TKeyThe key being added.
value
TValueThe value being added.
Remarks
This method is intended for derived classes to implement custom logic before an addition occurs.
OnCleared()
Called after all items have been cleared from the collection.
protected virtual void OnCleared()
Remarks
This method is intended for derived classes to implement custom logic after clearing occurs.
OnClearing()
Called before clearing all items from the collection.
protected virtual void OnClearing()
Remarks
This method is intended for derived classes to implement custom logic before clearing occurs.
OnRemoved(TKey, TValue)
Called after a key-value pair has been removed from the collection.
protected virtual void OnRemoved(TKey key, TValue value)
Parameters
key
TKeyThe key that was removed.
value
TValueThe value that was removed.
Remarks
This method is intended for derived classes to implement custom logic after a removal occurs.
OnRemoving(TKey, TValue)
Called before removing a key-value pair from the collection.
protected virtual void OnRemoving(TKey key, TValue value)
Parameters
key
TKeyThe key being removed.
value
TValueThe value being removed.
Remarks
This method is intended for derived classes to implement custom logic before a removal occurs.
OnSetted(TKey, TValue)
Called after a new value has been set for an existing key.
protected virtual void OnSetted(TKey key, TValue value)
Parameters
key
TKeyThe key that was updated.
value
TValueThe new value that was set.
Remarks
This method is intended for derived classes to implement custom logic after a value is updated.
OnSetting(TKey, TValue)
Called before setting a new value for an existing key.
protected virtual void OnSetting(TKey key, TValue value)
Parameters
key
TKeyThe key being updated.
value
TValueThe new value to set.
Remarks
This method is intended for derived classes to implement custom logic before a value is updated.
Remove(KeyValuePair<TKey, TValue>)
Removes a specific key-value pair from the collection.
public bool Remove(KeyValuePair<TKey, TValue> item)
Parameters
item
KeyValuePair<TKey, TValue>The key-value pair to remove.
Returns
- bool
True if the pair was found and removed; otherwise, false.
Remarks
This implementation only considers the key for removal, ignoring the value in item
.
Remove(TKey)
Removes the value with the specified key from the collection.
public virtual bool Remove(TKey key)
Parameters
key
TKeyThe key of the element to remove.
Returns
- bool
True if the element was found and removed; otherwise, false.
TryGetValue(TKey, out TValue)
Attempts to retrieve the value associated with the specified key.
public virtual bool TryGetValue(TKey key, out TValue value)
Parameters
key
TKeyThe key to locate.
value
TValueWhen this method returns, contains the value associated with the key if found; otherwise, the default value of
TValue
.
Returns
- bool
True if the key was found; otherwise, false.