Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Static Public Member Functions | List of all members
Loyc.Collections.DictionaryExt Class Reference

Extension methods for Dictionary{K,V}, IDictionary{K,V} and IDictionaryEx{K, V}. More...


Source files:

Remarks

Extension methods for Dictionary{K,V}, IDictionary{K,V} and IDictionaryEx{K, V}.

Static Public Member Functions

static IDictionarySink< K, V > AsSink< K, V > (this IDictionary< K, V > dict)
 Adapts a dictionary to the IDictionarySink{K, V} interface. More...
 
static V GetOrAdd< K, V > (this IDictionary< K, V > dict, K key, V value)
 Adds a key/value pair to the dictionary if the key is not already present, and returns the existing or new value. More...
 
static V GetOrAdd< K, V > (this IDictionary< K, V > dict, K key, Func< K, V > valueFactory)
 Adds a key/value pair to the dictionary if the key is not already present, by using the specified function to obtain a value, and returns the existing or new value. More...
 
static V GetOrAdd< K, V > (this Dictionary< K, V > dict, K key, V value)
 
static V GetOrAdd< K, V > (this Dictionary< K, V > dict, K key, Func< K, V > valueFactory)
 
static V AddOrUpdate< K, V > (this IDictionary< K, V > dict, K key, Func< K, V > addValueFactory, Func< K, V, V > updateValueFactory)
 Uses the specified functions either to add a key/value pair to the dictionary if the key does not already exist, or to update a key/value pair in the dictionary if the key already exists. More...
 
static V AddOrUpdate< K, V > (this IDictionary< K, V > dict, K key, V addValue, Func< K, V, V > updateValueFactory)
 Adds a key/value pair to the dictionary if the key does not already exist, or, if it does, updates a key/value pair in the dictionary using the specified function. More...
 
static V AddOrUpdate< K, V > (this Dictionary< K, V > dict, K key, Func< K, V > addValueFactory, Func< K, V, V > updateValueFactory)
 
static V AddOrUpdate< K, V > (this Dictionary< K, V > dict, K key, V addValue, Func< K, V, V > updateValueFactory)
 
static V TryGetValue< K, V > (this Dictionary< K, V > dict, K key, V defaultValue)
 An alternate version TryGetValue that returns a default value if the key was not found in the dictionary, and that does not throw if the key is null. More...
 
static V TryGetValue< K, V > (this IDictionary< K, V > dict, K key, V defaultValue)
 
static V TryGetValue< K, V > (this IReadOnlyDictionary< K, V > dict, K key, V defaultValue)
 
static V TryGetValue< K, V > (this IDictionaryAndReadOnly< K, V > dict, K key, V defaultValue)
 Disambiguating overload. More...
 
static Maybe< V > TryGetValue< K, V > (this IDictionary< K, V > dict, K key)
 Same as IDictionary.TryGetValue() except that this method does not throw an exception when key==null (it simply returns NoValue), and it returns the result as Maybe{V} instead of storing the result in an "out" parameter. More...
 
static Maybe< V > TryGetValue< K, V > (this IReadOnlyDictionary< K, V > dict, K key)
 
static Maybe< V > TryGetValue< K, V > (this IDictionaryAndReadOnly< K, V > dict, K key)
 
static Maybe< V > TryGetValue< K, V > (this Dictionary< K, V > dict, K key)
 
static bool TryGetValueSafe< K, V > (this IDictionary< K, V > dict, K key, [MaybeNullWhen(false)] out V value)
 Same as IDictionary.TryGetValue() except that this method does not throw an exception when key==null (it simply returns false). More...
 
static int AddRange< K, V > (this IDictionary< K, V > dict, IEnumerable< KeyValuePair< K, V >> list)
 Adds data to a dictionary (dict.Add(key, value) for all pairs in a sequence.) More...
 
static void SetRange< K, V > (this IDictionary< K, V > dict, IEnumerable< KeyValuePair< K, V >> list)
 Adds data to a dictionary (dict[key] = value for all pairs in a sequence.) More...
 
static int RemoveRange< K, V > (this IDictionary< K, V > dict, IEnumerable< K > list)
 Tries to remove a set of key-values from a dictionary based on their keys. More...
 
static int AddRange< K, V > (this IDictionary< K, V > dict, IEnumerable< KeyValuePair< K, V >> data, DictEditMode mode)
 Default implementation of IDictionaryEx{K, V}.AddRange. Merges the contents of the specified sequence into this map. More...
 
static Maybe< V > GetAndRemove< K, V > (this IDictionary< K, V > dict, K key)
 Default implementation of IDictionaryEx{K, V}.GetAndRemove. Gets the value associated with the specified key, then removes the pair with that key from the dictionary. More...
 
static Maybe< V > GetAndRemove< K, V > (this Dictionary< K, V > dict, K key)
 

Member Function Documentation

static V Loyc.Collections.DictionaryExt.AddOrUpdate< K, V > ( this IDictionary< K, V >  dict,
key,
Func< K, V >  addValueFactory,
Func< K, V, V >  updateValueFactory 
)
inlinestatic

Uses the specified functions either to add a key/value pair to the dictionary if the key does not already exist, or to update a key/value pair in the dictionary if the key already exists.

Returns
The new value associated with the key, which is either the result of addValueFactory or updateValueFactory.

This is not thread-safe. Only one thread should access the dictionary at once.

static V Loyc.Collections.DictionaryExt.AddOrUpdate< K, V > ( this IDictionary< K, V >  dict,
key,
addValue,
Func< K, V, V >  updateValueFactory 
)
inlinestatic

Adds a key/value pair to the dictionary if the key does not already exist, or, if it does, updates a key/value pair in the dictionary using the specified function.

Returns
The new value associated with the key, which is either addValue or the result of updateValueFactory.

This is not thread-safe. Only one thread should access the dictionary at once.

static V Loyc.Collections.DictionaryExt.AddOrUpdate< K, V > ( this Dictionary< K, V >  dict,
key,
Func< K, V >  addValueFactory,
Func< K, V, V >  updateValueFactory 
)
inlinestatic

This overload is preferred over the IDictionary{K,V} version because on .NET 6+ it needs only a single hash lookup. The factory functions must not modify the dictionary.

static V Loyc.Collections.DictionaryExt.AddOrUpdate< K, V > ( this Dictionary< K, V >  dict,
key,
addValue,
Func< K, V, V >  updateValueFactory 
)
inlinestatic

This overload is preferred over the IDictionary{K,V} version because on .NET 6+ it needs only a single hash lookup. The update function must not modify the dictionary.

static int Loyc.Collections.DictionaryExt.AddRange< K, V > ( this IDictionary< K, V >  dict,
IEnumerable< KeyValuePair< K, V >>  list 
)
inlinestatic

Adds data to a dictionary (dict.Add(key, value) for all pairs in a sequence.)

static int Loyc.Collections.DictionaryExt.AddRange< K, V > ( this IDictionary< K, V >  dict,
IEnumerable< KeyValuePair< K, V >>  data,
DictEditMode  mode 
)
inlinestatic

Default implementation of IDictionaryEx{K, V}.AddRange. Merges the contents of the specified sequence into this map.

static IDictionarySink<K, V> Loyc.Collections.DictionaryExt.AsSink< K, V > ( this IDictionary< K, V >  dict)
static

Adapts a dictionary to the IDictionarySink{K, V} interface.

static Maybe<V> Loyc.Collections.DictionaryExt.GetAndRemove< K, V > ( this IDictionary< K, V >  dict,
key 
)
inlinestatic

Default implementation of IDictionaryEx{K, V}.GetAndRemove. Gets the value associated with the specified key, then removes the pair with that key from the dictionary.

static Maybe<V> Loyc.Collections.DictionaryExt.GetAndRemove< K, V > ( this Dictionary< K, V >  dict,
key 
)
inlinestatic

This overload is preferred over the IDictionary{K,V} version because it needs only a single hash lookup on runtimes that offer Dictionary.Remove(key, out value).

static V Loyc.Collections.DictionaryExt.GetOrAdd< K, V > ( this IDictionary< K, V >  dict,
key,
value 
)
inlinestatic

Adds a key/value pair to the dictionary if the key is not already present, and returns the existing or new value.

Returns
The existing value (if the key already existed) or the new value.

This is not thread-safe. Only one thread should access the dictionary at once.

static V Loyc.Collections.DictionaryExt.GetOrAdd< K, V > ( this IDictionary< K, V >  dict,
key,
Func< K, V >  valueFactory 
)
inlinestatic

Adds a key/value pair to the dictionary if the key is not already present, by using the specified function to obtain a value, and returns the existing or new value.

Returns
The existing value (if the key already existed) or the new value.

This is not thread-safe. Only one thread should access the dictionary at once.

static V Loyc.Collections.DictionaryExt.GetOrAdd< K, V > ( this Dictionary< K, V >  dict,
key,
value 
)
inlinestatic

This overload is preferred over the IDictionary{K,V} version because on .NET 6+ it needs only a single hash lookup.

static V Loyc.Collections.DictionaryExt.GetOrAdd< K, V > ( this Dictionary< K, V >  dict,
key,
Func< K, V >  valueFactory 
)
inlinestatic

This overload is preferred over the IDictionary{K,V} version because on .NET 6+ it needs only a single hash lookup.

static int Loyc.Collections.DictionaryExt.RemoveRange< K, V > ( this IDictionary< K, V >  dict,
IEnumerable< K >  list 
)
inlinestatic

Tries to remove a set of key-values from a dictionary based on their keys.

Returns
The number of keys that were found and removed.
static void Loyc.Collections.DictionaryExt.SetRange< K, V > ( this IDictionary< K, V >  dict,
IEnumerable< KeyValuePair< K, V >>  list 
)
inlinestatic

Adds data to a dictionary (dict[key] = value for all pairs in a sequence.)

static V Loyc.Collections.DictionaryExt.TryGetValue< K, V > ( this Dictionary< K, V >  dict,
key,
defaultValue 
)
inlinestatic

An alternate version TryGetValue that returns a default value if the key was not found in the dictionary, and that does not throw if the key is null.

Returns
The value associated with the specified key, or defaultValue if no value is associated with the key.
static V Loyc.Collections.DictionaryExt.TryGetValue< K, V > ( this IDictionaryAndReadOnly< K, V >  dict,
key,
defaultValue 
)
static

Disambiguating overload.

static Maybe<V> Loyc.Collections.DictionaryExt.TryGetValue< K, V > ( this IDictionary< K, V >  dict,
key 
)
inlinestatic

Same as IDictionary.TryGetValue() except that this method does not throw an exception when key==null (it simply returns NoValue), and it returns the result as Maybe{V} instead of storing the result in an "out" parameter.

static bool Loyc.Collections.DictionaryExt.TryGetValueSafe< K, V > ( this IDictionary< K, V >  dict,
key,
[MaybeNullWhen(false)] out V  value 
)
inlinestatic

Same as IDictionary.TryGetValue() except that this method does not throw an exception when key==null (it simply returns false).