Class SynchronizedPairSet<TKey, TValue>
- Namespace
- Ecng.Collections
- Assembly
- Ecng.Collections.dll
Represents a thread-safe collection that maintains key/value pairs and allows bidirectional lookups.
public class SynchronizedPairSet<TKey, TValue> : SynchronizedKeyedCollection<TKey, TValue>, IDictionary<TKey, TValue>, ISynchronizedCollection<KeyValuePair<TKey, TValue>>, ISynchronizedCollection, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
TKey
TValue
- Inheritance
-
KeyedCollection<TKey, TValue>SynchronizedKeyedCollection<TKey, TValue>SynchronizedPairSet<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ISynchronizedCollection<KeyValuePair<TKey, TValue>>ICollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Derived
- Inherited Members
- Extension Methods
Constructors
SynchronizedPairSet()
Initializes a new instance of the SynchronizedPairSet<TKey, TValue> class.
public SynchronizedPairSet()
SynchronizedPairSet(IEqualityComparer<TKey>)
Initializes a new instance of the SynchronizedPairSet<TKey, TValue> class with a specified comparer for keys.
public SynchronizedPairSet(IEqualityComparer<TKey> comparer)
Parameters
comparer
IEqualityComparer<TKey>An equality comparer for keys.
SynchronizedPairSet(IEqualityComparer<TKey>, IEqualityComparer<TValue>)
Initializes a new instance of the SynchronizedPairSet<TKey, TValue> class with specified comparers for keys and values.
public SynchronizedPairSet(IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
Parameters
keyComparer
IEqualityComparer<TKey>An equality comparer for keys.
valueComparer
IEqualityComparer<TValue>An equality comparer for values.
Properties
this[TValue]
Gets the key associated with the specified value.
public TKey this[TValue value] { get; }
Parameters
value
TValueThe value for which to retrieve the key.
Property Value
- TKey
The key corresponding to the specified value.
Methods
Add(TKey, TValue)
Adds the specified key and value to the collection.
public override void Add(TKey key, TValue value)
Parameters
key
TKeyThe key to insert.
value
TValueThe value to insert.
CanAdd(TKey, TValue)
Determines whether a key/value pair can be added based on whether the value is already present.
protected override bool CanAdd(TKey key, TValue value)
Parameters
key
TKeyThe key to be added.
value
TValueThe value to be added.
Returns
- bool
true
if the value is not already contained; otherwisefalse
.
ContainsValue(TValue)
Determines whether the collection contains an entry with the specified value.
public bool ContainsValue(TValue value)
Parameters
value
TValueThe value to locate.
Returns
- bool
true
if the value is found; otherwisefalse
.
GetKey(TValue)
Retrieves the key associated with the specified value.
public TKey GetKey(TValue value)
Parameters
value
TValueThe value whose key is requested.
Returns
- TKey
The key associated with the given value.
GetValue(TKey)
Retrieves the value associated with the specified key.
public TValue GetValue(TKey key)
Parameters
key
TKeyThe key whose value is requested.
Returns
- TValue
The value associated with the given key.
OnAdding(TKey, TValue)
Called when an item is being added. Adds the value/key mapping to the internal dictionary.
protected override void OnAdding(TKey key, TValue value)
Parameters
key
TKeyThe key being added.
value
TValueThe value being added.
OnClearing()
Called when the collection is being cleared. Clears the internal dictionary.
protected override void OnClearing()
OnRemoving(TKey, TValue)
Called when an item is being removed. Removes the value/key mapping from the internal dictionary.
protected override void OnRemoving(TKey key, TValue value)
Parameters
key
TKeyThe key being removed.
value
TValueThe value being removed.
OnSetting(TKey, TValue)
Called when an existing item is being set. Updates the value/key mapping in the internal dictionary.
protected override void OnSetting(TKey key, TValue value)
Parameters
key
TKeyThe key being set.
value
TValueThe value being set.
RemoveByValue(TValue)
Removes the element with the specified value if it is found.
public bool RemoveByValue(TValue value)
Parameters
value
TValueThe value to remove.
Returns
- bool
true
if the item was removed; otherwisefalse
.
SetKey(TKey, TValue)
Removes the old pair for the specified key and adds the new key/value pair. Raises an event when setting.
public void SetKey(TKey key, TValue value)
Parameters
key
TKeyThe key to set.
value
TValueThe value to associate with the key.
SetValue(TKey, TValue)
Updates the value associated with a key.
public void SetValue(TKey key, TValue value)
Parameters
key
TKeyThe key to update.
value
TValueThe new value.
TryAdd(TKey, TValue)
Attempts to add a new key/value pair if neither key nor value is already present.
public bool TryAdd(TKey key, TValue value)
Parameters
key
TKeyThe key to add.
value
TValueThe value to add.
Returns
- bool
true
if the pair was added; otherwisefalse
.
TryGetKey(TValue)
Attempts to get a key for the specified value.
public TKey TryGetKey(TValue value)
Parameters
value
TValueThe value whose key is requested.
Returns
- TKey
The key if found; otherwise the default.
TryGetKey(TValue, out TKey)
Attempts to get a key for the specified value, returning a boolean indicating success.
public bool TryGetKey(TValue value, out TKey key)
Parameters
value
TValueThe value whose key is requested.
key
TKeyWhen this method returns, contains the key if found.
Returns
- bool
true
if the key was found; otherwisefalse
.
TryGetKeyAndRemove(TValue, out TKey)
Attempts to get and remove the key associated with the specified value.
public bool TryGetKeyAndRemove(TValue value, out TKey key)
Parameters
value
TValueThe value for which to retrieve and remove the key.
key
TKeyWhen this method returns, contains the removed key if found.
Returns
- bool
true
if the key was removed successfully; otherwisefalse
.