Table of Contents

Class ConcurrentDictionaryExtensions

Namespace
Plugin.BaseTypeExtensions
Assembly
Plugin.BaseTypeExtensions.dll

Extension methods for ConcurrentDictionary<TKey, TValue>.

public static class ConcurrentDictionaryExtensions
Inheritance
ConcurrentDictionaryExtensions
Inherited Members

Methods

TryAddAsync<TKey, TValue>(ConcurrentDictionary<TKey, TValue>, TKey, TValue, int, TimeSpan?, CancellationToken)

Attempts to add the specified key and value to the dictionary with retry logic. Retries the operation if it fails, with an optional delay between attempts.

public static Task<bool> TryAddAsync<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, TValue value, int retryCount = 3, TimeSpan? delayBetweenRetries = null, CancellationToken cancellationToken = default) where TKey : notnull

Parameters

dictionary ConcurrentDictionary<TKey, TValue>

The concurrent dictionary to add to. Cannot be null.

key TKey

The key of the element to add. Cannot be null.

value TValue

The value of the element to add.

retryCount int

The number of retry attempts. Must be greater than or equal to 1. Default is 3.

delayBetweenRetries TimeSpan?

The delay between retry attempts. If null, no delay is applied. Default is null.

cancellationToken CancellationToken

The cancellation token to observe. Default is None.

Returns

Task<bool>

A task that represents the asynchronous operation. The task result is true if the key/value pair was added successfully; false if the key already exists or all retry attempts failed.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Exceptions

ArgumentNullException

Thrown if dictionary or key is null.

ArgumentOutOfRangeException

Thrown if retryCount is less than 1.

OperationCanceledException

Thrown if the operation is canceled via cancellationToken.

TryGetValueAsync<TKey, TValue>(ConcurrentDictionary<TKey, TValue>, TKey, int, TimeSpan?, CancellationToken)

Attempts to get the value associated with the specified key with retry logic. Retries the operation if it fails, with an optional delay between attempts.

public static Task<(bool Success, TValue? Value)> TryGetValueAsync<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, int retryCount = 3, TimeSpan? delayBetweenRetries = null, CancellationToken cancellationToken = default) where TKey : notnull

Parameters

dictionary ConcurrentDictionary<TKey, TValue>

The concurrent dictionary to get the value from. Cannot be null.

key TKey

The key of the value to get. Cannot be null.

retryCount int

The number of retry attempts. Must be greater than or equal to 1. Default is 3.

delayBetweenRetries TimeSpan?

The delay between retry attempts. If null, no delay is applied. Default is null.

cancellationToken CancellationToken

The cancellation token to observe. Default is None.

Returns

Task<(bool Success, TValue Value)>

A task that represents the asynchronous operation. The task result is a tuple containing:

  • Success: true if the key was found; otherwise, false.
  • Value: The value associated with the key if found; otherwise, default(TValue).

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Exceptions

ArgumentNullException

Thrown if dictionary or key is null.

ArgumentOutOfRangeException

Thrown if retryCount is less than 1.

OperationCanceledException

Thrown if the operation is canceled via cancellationToken.

TryRemoveAsync<TKey, TValue>(ConcurrentDictionary<TKey, TValue>, TKey, int, TimeSpan?, CancellationToken)

Attempts to remove the value with the specified key from the dictionary with retry logic. Retries the operation if it fails, with an optional delay between attempts.

public static Task<(bool Success, TValue? Value)> TryRemoveAsync<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, int retryCount = 3, TimeSpan? delayBetweenRetries = null, CancellationToken cancellationToken = default) where TKey : notnull

Parameters

dictionary ConcurrentDictionary<TKey, TValue>

The concurrent dictionary to remove from. Cannot be null.

key TKey

The key of the element to remove. Cannot be null.

retryCount int

The number of retry attempts. Must be greater than or equal to 1. Default is 3.

delayBetweenRetries TimeSpan?

The delay between retry attempts. If null, no delay is applied. Default is null.

cancellationToken CancellationToken

The cancellation token to observe. Default is None.

Returns

Task<(bool Success, TValue Value)>

A task that represents the asynchronous operation. The task result is a tuple containing:

  • Success: true if the element was removed successfully; otherwise, false.
  • Value: The removed value if successful; otherwise, default(TValue).

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Exceptions

ArgumentNullException

Thrown if dictionary or key is null.

ArgumentOutOfRangeException

Thrown if retryCount is less than 1.

OperationCanceledException

Thrown if the operation is canceled via cancellationToken.

TryUpdateAsync<TKey, TValue>(ConcurrentDictionary<TKey, TValue>, TKey, TValue, TValue, int, TimeSpan?, CancellationToken)

Attempts to update the value associated with the specified key with retry logic. Retries the operation if it fails, with an optional delay between attempts.

public static Task<bool> TryUpdateAsync<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, TValue newValue, TValue comparisonValue, int retryCount = 3, TimeSpan? delayBetweenRetries = null, CancellationToken cancellationToken = default) where TKey : notnull

Parameters

dictionary ConcurrentDictionary<TKey, TValue>

The concurrent dictionary to update. Cannot be null.

key TKey

The key of the value to update. Cannot be null.

newValue TValue

The value that replaces the value of the element with key if the comparison results in equality.

comparisonValue TValue

The value that is compared to the value of the element with key.

retryCount int

The number of retry attempts. Must be greater than or equal to 1. Default is 3.

delayBetweenRetries TimeSpan?

The delay between retry attempts. If null, no delay is applied. Default is null.

cancellationToken CancellationToken

The cancellation token to observe. Default is None.

Returns

Task<bool>

A task that represents the asynchronous operation. The task result is true if the value with key was equal to comparisonValue and replaced with newValue; otherwise, false.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Exceptions

ArgumentNullException

Thrown if dictionary or key is null.

ArgumentOutOfRangeException

Thrown if retryCount is less than 1.

OperationCanceledException

Thrown if the operation is canceled via cancellationToken.