Table of Contents

Class PairSet<TKey, TValue>

Namespace
Ecng.Collections
Assembly
Ecng.Collections.dll

Represents a collection of key-value pairs that supports bidirectional lookup by key or value.

public sealed class PairSet<TKey, TValue> : 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>
PairSet<TKey, TValue>
Implements
IDictionary<TKey, TValue>
ICollection<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>
Inherited Members
Extension Methods

Constructors

PairSet()

Initializes a new instance of the PairSet<TKey, TValue> class with default comparers.

public PairSet()

PairSet(IEqualityComparer<TKey>)

Initializes a new instance of the PairSet<TKey, TValue> class with a specified key comparer.

public PairSet(IEqualityComparer<TKey> comparer)

Parameters

comparer IEqualityComparer<TKey>

The equality comparer for keys, or null to use the default comparer.

PairSet(IEqualityComparer<TKey>, IEqualityComparer<TValue>)

Initializes a new instance of the PairSet<TKey, TValue> class with specified key and value comparers.

public PairSet(IEqualityComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)

Parameters

keyComparer IEqualityComparer<TKey>

The equality comparer for keys, or null to use the default comparer.

valueComparer IEqualityComparer<TValue>

The equality comparer for values, or null to use the default comparer.

Properties

this[TValue]

Gets the key associated with the specified value.

public TKey this[TValue value] { get; }

Parameters

value TValue

The value to look up.

Property Value

TKey

The key associated with the specified value.

Exceptions

KeyNotFoundException

Thrown when the value is not found in the collection.

Methods

CanAdd(TKey, TValue)

Determines whether a key-value pair can be added to the collection.

protected override bool CanAdd(TKey key, TValue value)

Parameters

key TKey

The key to check.

value TValue

The value to check.

Returns

bool

True if the value is not already associated with a key; otherwise, false.

ContainsValue(TValue)

Determines whether the collection contains a specific value.

public bool ContainsValue(TValue value)

Parameters

value TValue

The value to check.

Returns

bool

True if the value exists in the collection; otherwise, false.

GetKey(TValue)

Retrieves the key associated with the specified value.

public TKey GetKey(TValue value)

Parameters

value TValue

The value to look up.

Returns

TKey

The key associated with the specified value.

Exceptions

KeyNotFoundException

Thrown when the value is not found in the collection.

GetValue(TKey)

Retrieves the value associated with the specified key.

public TValue GetValue(TKey key)

Parameters

key TKey

The key to look up.

Returns

TValue

The value associated with the specified key.

Exceptions

KeyNotFoundException

Thrown when the key is not found in the collection.

OnAdding(TKey, TValue)

Called before adding a key-value pair to the collection, updating the value-to-key mapping.

protected override void OnAdding(TKey key, TValue value)

Parameters

key TKey

The key being added.

value TValue

The value being added.

OnClearing()

Called before clearing the collection, clearing the value-to-key mapping.

protected override void OnClearing()

OnRemoving(TKey, TValue)

Called before removing a key-value pair, updating the value-to-key mapping.

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 before setting a new value for an existing key, updating the value-to-key mapping.

protected override void OnSetting(TKey key, TValue value)

Parameters

key TKey

The key being updated.

value TValue

The new value to set.

RemoveByValue(TValue)

Removes a key-value pair from the collection based on the specified value.

public bool RemoveByValue(TValue value)

Parameters

value TValue

The value to remove.

Returns

bool

True if the value was found and removed; otherwise, false.

SetKey(TValue, TKey)

Sets a new key for an existing value, replacing any previous association.

public void SetKey(TValue value, TKey key)

Parameters

value TValue

The value to associate with the new key.

key TKey

The new key to associate with the value.

SetValue(TKey, TValue)

Sets a new value for an existing key, updating the internal value-to-key mapping.

public void SetValue(TKey key, TValue value)

Parameters

key TKey

The key whose value is to be updated.

value TValue

The new value to associate with the key.

TryAdd(TKey, TValue)

Attempts to add a key-value pair to the collection if neither the key nor value already exists.

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; false if either the key or value already exists.

TryGetKey(TValue, out TKey)

Attempts to retrieve the key associated with a specified value.

public bool TryGetKey(TValue value, out TKey key)

Parameters

value TValue

The value to look up.

key TKey

When this method returns, contains the key if found; otherwise, the default value of TKey.

Returns

bool

True if the value was found; otherwise, false.