Hi,
I was asked whats wrong/how can the following scenario can be fixed
Customer customer = null;
customer.WhenNull(c => new Customer())
.Foo();
// instead of
Customer customer = null;
if (customer == null)
{
customer = new Customer();
}
customer.Foo();
One developer sends me his Version of the WhenNull extension
public static T WhenNull<T>(this T source, Action<T> nullAction)
{
if (source != null)
{
return source;
}
if (nullAction == null)
{
throw new ArgumentNullException("nullAction");
}
nullAction(source);
return source;
}
His problem / intention is, that he doesn't want to specify the underlying object in the lambda expression (in this case "customer")
Customer customer = null;
customer.WhenNull(c => customer = new Customer())
.Foo();
I thought this can't be done.
Is this correct?