Table of Contents

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>
SynchronizedPairSet<TKey, TValue>
Implements
IDictionary<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 TValue

The 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 TKey

The key to insert.

value TValue

The 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 TKey

The key to be added.

value TValue

The value to be added.

Returns

bool

true if the value is not already contained; otherwise false.

ContainsValue(TValue)

Determines whether the collection contains an entry with the specified value.

public bool ContainsValue(TValue value)

Parameters

value TValue

The value to locate.

Returns

bool

true if the value is found; otherwise false.

GetKey(TValue)

Retrieves the key associated with the specified value.

public TKey GetKey(TValue value)

Parameters

value TValue

The 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 TKey

The 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 TKey

The key being added.

value TValue

The 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 TKey

The key being removed.

value TValue

The 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 TKey

The key being set.

value TValue

The value being set.

RemoveByValue(TValue)

Removes the element with the specified value if it is found.

public bool RemoveByValue(TValue value)

Parameters

value TValue

The value to remove.

Returns

bool

true if the item was removed; otherwise false.

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 TKey

The key to set.

value TValue

The 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 TKey

The key to update.

value TValue

The 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 TKey

The key to add.

value TValue

The value to add.

Returns

bool

true if the pair was added; otherwise false.

TryGetKey(TValue)

Attempts to get a key for the specified value.

public TKey TryGetKey(TValue value)

Parameters

value TValue

The 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 TValue

The value whose key is requested.

key TKey

When this method returns, contains the key if found.

Returns

bool

true if the key was found; otherwise false.

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 TValue

The value for which to retrieve and remove the key.

key TKey

When this method returns, contains the removed key if found.

Returns

bool

true if the key was removed successfully; otherwise false.