Swiss Army Knife
General utility tools for Scada Minds
Static Public Member Functions | List of all members
DictionaryExtensions Class Reference

Extension methods for the BCL Dictionaries. More...

Static Public Member Functions

static TValue GetOrThrow< TKey, TValue > (this IReadOnlyDictionary< TKey, TValue > dictionary, TKey key, Func< Exception > errorCreator)
 Get the value for given dictionary key. If no value can be found, it will throw an exception generated from the errorCreator function. More...
 
static TValue GetValueOr< TKey, TValue > (this IReadOnlyDictionary< TKey, TValue > dictionary, TKey key, Func< TValue > valueProducer)
 Same as the Regular GetOrDefault except this one takes a factory to produce the value. More...
 
static Dictionary< TKey, TNewValue > SelectValues< TKey, TOldValue, TNewValue > (this IReadOnlyDictionary< TKey, TOldValue > dictionary, Func< TOldValue, TNewValue > selector)
 Returns a new dictionary after applying the provided function to each value from the given dictionary. More...
 
static async Task< Dictionary< TKey, TVal > > AwaitTasksInValuesAsync< TKey, TVal > (this IReadOnlyDictionary< TKey, Task< TVal >> dictionaryToAwait)
 Given a dictionary of <TKey, Task<TVal>> it awaits all the Tasks in parallel, and constructs a new dictionary<TKey, TValgt;. More...
 

Detailed Description

Extension methods for the BCL Dictionaries.

Member Function Documentation

◆ AwaitTasksInValuesAsync< TKey, TVal >()

static async Task<Dictionary<TKey, TVal> > AwaitTasksInValuesAsync< TKey, TVal > ( this IReadOnlyDictionary< TKey, Task< TVal >>  dictionaryToAwait)
inlinestatic

Given a dictionary of <TKey, Task<TVal>> it awaits all the Tasks in parallel, and constructs a new dictionary<TKey, TValgt;.

Parameters
dictionaryToAwaitThe dictionary with tasks to await.

///

Template Parameters
TKeyType of the dictionaries keys.
TValType of the dictionaries values.
Returns
A new dictionary with all the Tasks in the values turned into their actual values.
Type Constraints
TKey :notnull 

◆ GetOrThrow< TKey, TValue >()

static TValue GetOrThrow< TKey, TValue > ( this IReadOnlyDictionary< TKey, TValue >  dictionary,
TKey  key,
Func< Exception >  errorCreator 
)
inlinestatic

Get the value for given dictionary key. If no value can be found, it will throw an exception generated from the errorCreator function.

var myDictionary = new Dictionary<string, string> {
{"myKey", "myValue"}
}
// Will return "myValue"
myDictionary.GetOrThrow("myKey", () => new ArgumentException("tried to get invalid key"));
// Will throw ArgumentException
myDictionary.GetOrThrow("nonExistingKey", () => new ArgumentException("tried to get invalid key"));
Type Constraints
TKey :notnull 

◆ GetValueOr< TKey, TValue >()

static TValue GetValueOr< TKey, TValue > ( this IReadOnlyDictionary< TKey, TValue >  dictionary,
TKey  key,
Func< TValue >  valueProducer 
)
inlinestatic

Same as the Regular GetOrDefault except this one takes a factory to produce the value.

Type Constraints
TKey :notnull 

◆ SelectValues< TKey, TOldValue, TNewValue >()

static Dictionary<TKey, TNewValue> SelectValues< TKey, TOldValue, TNewValue > ( this IReadOnlyDictionary< TKey, TOldValue >  dictionary,
Func< TOldValue, TNewValue >  selector 
)
inlinestatic

Returns a new dictionary after applying the provided function to each value from the given dictionary.

Template Parameters
TKeyKey Type.
TOldValueOriginal Type.
TNewValueNew Type.
Parameters
dictionarySource Dictionary.
selectorSelector function.
// Will return a new dictionary where value type is integer
var myDictionary = new Dictionary<string, string> {
{"myKey", "2"}
}
myDictionary.SelectValues(oldValue => int.Parse(oldValue, CultureInfo.InvariantCulture));
// Will throw exception
var myDictionary = new Dictionary<string, string> {
{"myKey", "bar"}
}
myDictionary.SelectValues(oldValue => int.Parse(oldValue, CultureInfo.InvariantCulture));
Returns
Dictioanary<TKey, TNewValue>.

The documentation for this class was generated from the following file: