Enhanced C#
Language of your choice: library documentation
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 file:

Remarks

Extension methods for Dictionary<K,V>, IDictionary<K,V> and IDictionaryEx<K, V>.

Static Public Member Functions

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 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 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 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, 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...
 

Member Function Documentation

◆ AddOrUpdate< K, V >() [1/2]

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.

◆ AddOrUpdate< K, V >() [2/2]

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.

◆ AddRange< K, V >() [1/2]

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.

◆ AddRange< K, V >() [2/2]

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.)

◆ GetAndRemove< K, V >()

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.

◆ GetOrAdd< K, V >() [1/2]

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.

◆ GetOrAdd< K, V >() [2/2]

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.

◆ RemoveRange< K, V >()

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.

◆ SetRange< K, V >()

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.)

◆ TryGetValue< K, V >() [1/2]

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.

◆ TryGetValue< K, V >() [2/2]

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.

◆ TryGetValueSafe< K, V >()

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

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