Handy extension method for null-coalesing

Sunday, Apr 25, 2010 1 minute read Tags:
Hey, thanks for the interest in this post, but just letting you know that it is over 3 years old, so the content in here may not be accurate.

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 :).