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
dictionaryConcurrentDictionary<TKey, TValue>The concurrent dictionary to add to. Cannot be null.
keyTKeyThe key of the element to add. Cannot be null.
valueTValueThe value of the element to add.
retryCountintThe number of retry attempts. Must be greater than or equal to 1. Default is 3.
delayBetweenRetriesTimeSpan?The delay between retry attempts. If null, no delay is applied. Default is null.
cancellationTokenCancellationTokenThe cancellation token to observe. Default is None.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result is
trueif the key/value pair was added successfully;falseif the key already exists or all retry attempts failed.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Exceptions
- ArgumentNullException
Thrown if
dictionaryorkeyis null.- ArgumentOutOfRangeException
Thrown if
retryCountis 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
dictionaryConcurrentDictionary<TKey, TValue>The concurrent dictionary to get the value from. Cannot be null.
keyTKeyThe key of the value to get. Cannot be null.
retryCountintThe number of retry attempts. Must be greater than or equal to 1. Default is 3.
delayBetweenRetriesTimeSpan?The delay between retry attempts. If null, no delay is applied. Default is null.
cancellationTokenCancellationTokenThe 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:trueif the key was found; otherwise,false.Value: The value associated with the key if found; otherwise,default(TValue).
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Exceptions
- ArgumentNullException
Thrown if
dictionaryorkeyis null.- ArgumentOutOfRangeException
Thrown if
retryCountis 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
dictionaryConcurrentDictionary<TKey, TValue>The concurrent dictionary to remove from. Cannot be null.
keyTKeyThe key of the element to remove. Cannot be null.
retryCountintThe number of retry attempts. Must be greater than or equal to 1. Default is 3.
delayBetweenRetriesTimeSpan?The delay between retry attempts. If null, no delay is applied. Default is null.
cancellationTokenCancellationTokenThe 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:trueif the element was removed successfully; otherwise,false.Value: The removed value if successful; otherwise,default(TValue).
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Exceptions
- ArgumentNullException
Thrown if
dictionaryorkeyis null.- ArgumentOutOfRangeException
Thrown if
retryCountis 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
dictionaryConcurrentDictionary<TKey, TValue>The concurrent dictionary to update. Cannot be null.
keyTKeyThe key of the value to update. Cannot be null.
newValueTValueThe value that replaces the value of the element with
keyif the comparison results in equality.comparisonValueTValueThe value that is compared to the value of the element with
key.retryCountintThe number of retry attempts. Must be greater than or equal to 1. Default is 3.
delayBetweenRetriesTimeSpan?The delay between retry attempts. If null, no delay is applied. Default is null.
cancellationTokenCancellationTokenThe cancellation token to observe. Default is None.
Returns
- Task<bool>
A task that represents the asynchronous operation. The task result is
trueif the value withkeywas equal tocomparisonValueand replaced withnewValue; otherwise,false.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe type of the values in the dictionary.
Exceptions
- ArgumentNullException
Thrown if
dictionaryorkeyis null.- ArgumentOutOfRangeException
Thrown if
retryCountis less than 1.- OperationCanceledException
Thrown if the operation is canceled via
cancellationToken.