Table of Contents

Class DictionaryExtensions

Namespace
Olve.Utilities.CollectionExtensions
Assembly
Olve.Utilities.dll

Provides a set of static methods for querying and modifying objects that implement IDictionary<TKey, TValue>.

public static class DictionaryExtensions
Inheritance
DictionaryExtensions
Inherited Members

Remarks

These methods utilize GetValueRefOrAddDefault<TKey, TValue>(Dictionary<TKey, TValue>, TKey, out bool) and related APIs, which provide direct references to dictionary values. They should only be used in single-threaded contexts, as concurrent modifications can result in undefined behavior.

Methods

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

Gets the value associated with the specified key or adds a new value if the key does not exist.

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

Parameters

dictionary Dictionary<TKey, TValue>

The dictionary to get or add the value from.

key TKey

The key of the value to get or add.

valueFactory Func<TValue>

The function used to generate a new value for the key.

Returns

TValue

The value associated with the specified key or the newly added value.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

This method provides a direct reference to the dictionary's internal storage and should be used only in single-threaded scenarios.

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

Gets the value associated with the specified key or adds a new value if the key does not exist.

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 get or add the value from.

key TKey

The key of the value to get or add.

value TValue

The value to add if the key does not exist.

Returns

TValue

The value associated with the specified key or the newly added value.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

This method should be used in single-threaded contexts due to its direct manipulation of dictionary storage.

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

Updates the value associated with the specified key using a transformation function if the key exists.

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

Parameters

dictionary Dictionary<TKey, TValue>

The dictionary in which to update the value.

key TKey

The key of the value to update.

update Func<TValue, TValue>

The function used to update the existing value.

Returns

bool

true if the value was updated; otherwise, false.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

This method should be used only in single-threaded contexts, as it modifies dictionary values directly.

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

Updates the value associated with the specified key if the key exists.

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 in which to update the value.

key TKey

The key of the value to update.

value TValue

The new value to set.

Returns

bool

true if the value was updated; otherwise, false.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

This method should be used in single-threaded scenarios as it provides a direct reference to the dictionary's internal storage.