Skip to content

Latest commit

 

History

History
71 lines (51 loc) · 1.51 KB

README.md

File metadata and controls

71 lines (51 loc) · 1.51 KB

NotNull

Build status

A small library to help with nullable objects in C#.

Object Extension Methods

Extension methods with summaries and examples.

IfNotNull(this item, function)

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"

IfNotNullElseDefault(this item, function)

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"

IfNotNull(this item, action)

Runs the action when not null.

string testObject = null;
// Will not run
testObject.IfNotNull(s => Console.WriteLine("Hello"));

IfNotNullOrDefault(this item, function)

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

IfNotNullOrDefault(this item, action)

Runs the action when not null or default

string testObject = "Hello";
// Will run
testObject.IfNotNullOrDefault(s => Console.WriteLine(s));