IDictionaryExtensions Class
Definition
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
dictionaryIDictionary<TKey, TValue>The dictionary to insert into or update.
keyTKeyThe key to insert or update.
valueTValueThe value to associate with
key.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe 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
dictionaryis 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
dictionaryIDictionary<TKey, TValue>The dictionary to read from and, when necessary, insert into.
keyTKeyThe key to locate.
valueFactoryFunc<TKey, TValue>A delegate that produces the value to insert, given the key.
Returns
- TValue
The existing value when
keyis already present; otherwise the value produced byvalueFactory.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe 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
dictionaryorvalueFactoryis 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
dictionaryIDictionary<TKey, TValue>The dictionary to read from and, when necessary, insert into.
keyTKeyThe key to locate.
valueTValueThe value to insert when
keyis not present.
Returns
- TValue
The existing value when
keyis already present; otherwisevalue.
Type Parameters
TKeyThe type of the keys in the dictionary.
TValueThe 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
dictionaryis null.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |