Have you tried the null coalescing operator in C#??

It’s quite handy and can make the code a bit more lean if you need to do a lot of null value testing. Here is a simple example:

processCar = inputCar ?? new Car();

What it basically mean is “if inputCar is null then create a new car instead”.

The code can be unfolded in the following way:

processCar = inputCar != null ? inputCar : new Car();

…and then unfolded yet another step into simple if-else statements:

if (inputCar != null)
  processCar = inputCar;
  processCar = new Car();

This operator can also be chained into several sets of ?? where the expressions are tried one at the time. The first non-null value encountered will be used.

var theChosenCar = car1 ?? car2 ?? car3 ?? car4;

If all cars are null then theChosenCar will be null as well.

Handy, isn’t it??