A fluent alternative to foreach (var element in collection) { action(element); }
.
new [] { 1, 2, 3 }.ForEach(n => Console.WriteLine(n));
Retrieve a value from a dictionary, or a fallback value if key
is not present or is null (depending on acceptNulls
).
var i = new Dictionary<String, Int32> { { "a", 1 } };
i.GetOr("a", 2) // 1
i.GetOr("b", 2) // 2
i.GetOr("c", default) // 0
var o = new Dictionary<String, Object?> { { "a", "x" }, { "b", null } };
o.GetOr("a", "y") // "x"
o.GetOr("b", "y") // "y"
o.GetOr("b", "y", acceptNulls: true) // null
o.GetOr("c", default!) // null
Retrieve a value from a dictionary, adding it if key
is not present or is null (depending on acceptNulls
).
var d = new Dictionary<String, Int32?> { { "a", 1 }, { "b", null }, { "c", null } };
d.GetOrAdd("a", 0) // 1
d.GetOrAdd("b", 2) // 2
d.GetOrAdd("c", 3, acceptNulls: true) // null
A fluent alternative to Enumerable.Contains
.
Usage:
"x".IsOneOf(new List<String> { "a", "b", "c" }); // false
"b".IsOneOf("a", "b", "c"); // true
A fluent alternative to String.Join
.
Usage:
new []{"a","b","c"}.StringJoin(); // "abc"
new []{"a","b","c"}.StringJoin(", "); // "a, b, c"
Parameter-less conversion of any collection of key-value pairs to a dictionary.
new List<KeyValuePair<String, Int32>>{
new KeyValuePair<String, Int32>("a", 1)
}.toDictionary(); // { "a": 1 }
Contains shorthand methods and properties for instantiating common collections.
.E()
creates an empty Enumerable..C()
creates an empty Collection..L()
creates an empty List..S()
creates an empty HashSet..D()
creates an empty Dictionary.
*Str
properties for each type instantiate the corresponding collection of String
s. For Dictionary
it's DStr<TValue>
, which creates a dictionary of String
keys and TValue
values.