Handy extension method for null-coalesing

25 April 2010 ยท 1 minute read

Today a colleague asked me a question:

“How do you do a null-coalesce operator which will return a property of an object when not null?”

If you’re not familiar with the null coalesce operator it’s the ?? operator and it can be used for inline expressions when the test object is null.

You use it like so:

string test = null;
Console.WriteLine(test ?? "The string was null");

So it either returns itself or it returns your value, but what if you want to return a property of the object not itself, well you can’t use the ?? operator.

But never fear, extension methods are here! I wrote this quick little one for him:

public static TResult NullCoalese<TTarget, TResult>(this TTarget o, Func<TTarget, TResult> func, TResult whenNull) {
  return o == null ? whenNull : func(o);
}

Stick this in a namespace, maybe restrict the type of TTarget (or leave it as anything in .NET land, what ever takes your fancy, but if you don’t restrict it maybe don’t leave it in a common namespace!) and use it like this:

string test = null;
test.NullCoalese(x => Console.WriteLine(x), "Null was suppled");

Enjoy :).


Published: 2017-07-30 20:47:37 +1000 +1000, Version: bfd8bbd