Table of Contents

Class DictionaryExtensions

Namespace
LfrlAnvil.Extensions
Assembly
LfrlAnvil.Core.dll

Contains IDictionary<TKey, TValue> extension methods.

public static class DictionaryExtensions
Inheritance
DictionaryExtensions
Inherited Members

Methods

AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)

Adds a new entry or updates an existing one if key already exists.

public static AddOrUpdateResult AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)

Parameters

dictionary IDictionary<TKey, TValue>

Source dictionary.

key TKey

Entry's key.

value TValue

Entry's value.

Returns

AddOrUpdateResult

Added when new entry has been added (provided key did not exist), otherwise Updated.

Type Parameters

TKey

Dictionary key type.

TValue

Dictionary value type.

GetOrAddDefault<TKey, TValue>(IDictionary<TKey, TValue?>, TKey)

Reads a value associated with the specified key from the dictionary, if it exists. Otherwise adds a new entry to the dictionary with default value and returns that value.

public static TValue? GetOrAddDefault<TKey, TValue>(this IDictionary<TKey, TValue?> dictionary, TKey key) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>

Source dictionary.

key TKey

Entry's key.

Returns

TValue

Value associated with the provided key, if it exists, otherwise default value

Type Parameters

TKey

Dictionary key type.

TValue

Dictionary value type.

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

Reads a value associated with the specified key from the dictionary, if it exists. Otherwise adds a new entry to the dictionary with the value returned by the defaultValueProvider invocation and returns that value.

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

Parameters

dictionary IDictionary<TKey, TValue>

Source dictionary.

key TKey

Entry's key.

defaultValueProvider Func<TValue>

Entry's value provider. Gets invoked only when entry with the provided key does not exist in the dictionary.

Returns

TValue

Value associated with the provided key, if it exists, otherwise value returned by defaultValueProvider.

Type Parameters

TKey

Dictionary key type.

TValue

Dictionary value type.

GetOrAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Lazy<TValue>)

Reads a value associated with the specified key from the dictionary, if it exists. Otherwise adds a new entry to the dictionary with the value returned by the defaultValueProvider invocation and returns that value.

public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Lazy<TValue> defaultValueProvider) where TKey : notnull

Parameters

dictionary IDictionary<TKey, TValue>

Source dictionary.

key TKey

Entry's key.

defaultValueProvider Lazy<TValue>

Entry's value provider. Gets invoked only when entry with the provided key does not exist in the dictionary.

Returns

TValue

Value associated with the provided key, if it exists, otherwise value returned by defaultValueProvider.

Type Parameters

TKey

Dictionary key type.

TValue

Dictionary value type.

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

Attempts to update an existing entry in the dictionary with the specified key.

public static bool TryUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)

Parameters

dictionary IDictionary<TKey, TValue>

Source dictionary.

key TKey

Entry's key.

value TValue

Entry's value.

Returns

bool

true when entry with the specified key exists, otherwise false.

Type Parameters

TKey

Dictionary key type.

TValue

Dictionary value type.