Table of Contents

IDictionaryExtensions Class

Definition

Namespace
Bodu.Collections.Generic.Extensions
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
IDictionaryExtensions.AddOrUpdate.cs

Provides retrieval-and-mutation helpers for IDictionary<TKey, TValue> - get-or-add and add-or-update operations modelled on the ConcurrentDictionary<TKey, TValue> surface but available on any dictionary implementation.

public static class IDictionaryExtensions
Inheritance
IDictionaryExtensions
Inherited Members

Remarks

IDictionary<TKey, TValue> exposes only primitive lookup and mutation members, so callers routinely hand-roll the "return the existing value or insert a new one" pattern. This class collects those patterns behind verb-led names.

These helpers perform no synchronization. On a dictionary shared between threads the caller must provide external synchronization; they are not a substitute for ConcurrentDictionary<TKey, TValue> when concurrent access is required.

Methods

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

Inserts value under key, or replaces the existing value when the key is already present.

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

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to insert into or update.

key TKey

The key to insert or update.

value TValue

The value to associate with key.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

The operation is unconditional: it has the same effect as the indexer assignment dictionary[key] = value, and exists to state the add-or-update intent explicitly at the call site.

Exceptions

ArgumentNullException

Thrown when dictionary is null.

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

Returns the value associated with key, invoking valueFactory to produce and insert a value when the key is not already present.

public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to read from and, when necessary, insert into.

key TKey

The key to locate.

valueFactory Func<TKey, TValue>

A delegate that produces the value to insert, given the key.

Returns

TValue

The existing value when key is already present; otherwise the value produced by valueFactory.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

valueFactory is invoked at most once, and only when key is absent. The lookup and insert are not atomic; see the value-argument overload for the concurrency caveat.

Exceptions

ArgumentNullException

Thrown when dictionary or valueFactory is null.

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

Returns the value associated with key, adding value first when the key is not already present.

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

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to read from and, when necessary, insert into.

key TKey

The key to locate.

value TValue

The value to insert when key is not present.

Returns

TValue

The existing value when key is already present; otherwise value.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.

Remarks

The lookup and the conditional insert are performed as two separate operations; the helper provides no atomicity guarantee and must not be used as a substitute for GetOrAdd(TKey, TValue) on a shared instance.

Exceptions

ArgumentNullException

Thrown when dictionary is null.

Applies to

ProductVersions
.NET8, 10