A small library to help with nullable objects in C#.
Extension methods with summaries and examples.
Returns null
when null
, otherwise it returns the output of the applied function.
string testObject = null;
testObject.IfNotNull(s => s + "llo");
>> null
testObject = "He";
testObject.IfNotNull(s => s + "llo");
>> "Hello"
Returns default
value of TOut
when null
, otherwise it
returns the output of the applied function.
string testObject = null;
testObject.IfNotNullElseDefault(s => s + "llo");
>> null
testObject = "He";
testObject.IfNotNullElseDefault(s => s + "llo");
>> "Hello"
Runs the action when not null.
string testObject = null;
// Will not run
testObject.IfNotNull(s => Console.WriteLine("Hello"));
Returns default value of TOut
when null or
default, otherwise it returns the output of the applied function.
int testObject = 0;
testObject.IfNotNullOrDefault(i => i + 5);
>> 0
testObject = 1;
testObject.IfNotNullOrDefault(i => i + 5);
>> 6
Runs the action when not null or default
string testObject = "Hello";
// Will run
testObject.IfNotNullOrDefault(s => Console.WriteLine(s));