Table of Contents

Class DictionaryExtensions

Namespace
Utils.Collections
Assembly
Utils.dll

Provides thread-safe helper methods for working with Dictionary<TKey, TValue> instances.

public static class DictionaryExtensions
Inheritance
DictionaryExtensions
Inherited Members

Methods

GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TValue>)

Retrieves the value associated with key or adds a new value created by func in a thread-safe manner.

public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TValue> func) where TKey : notnull

Parameters

dictionary Dictionary<TKey, TValue>

The dictionary to operate on.

key TKey

Key of the element to retrieve.

func Func<TValue>

Factory used to create the value when absent.

Returns

TValue

The existing or newly added value.

Type Parameters

TKey

Key type.

TValue

Value type.

GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)

Retrieves the value associated with key or adds the provided value in a thread-safe manner.

public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull

Parameters

dictionary Dictionary<TKey, TValue>

The dictionary to operate on.

key TKey

Key of the element to retrieve.

value TValue

Value to add if the key is not present.

Returns

TValue

The existing or newly added value.

Type Parameters

TKey

Key type.

TValue

Value type.

TryUpdate<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)

Attempts to update an existing entry with the specified value in a thread-safe manner.

public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull

Parameters

dictionary Dictionary<TKey, TValue>

The dictionary to update.

key TKey

Key of the element to update.

value TValue

The new value.

Returns

bool

true if the key was present and the value updated.

Type Parameters

TKey

Key type.

TValue

Value type.