I was asked what was wrong / how the following scenario could be fixed
Customer customer = null;
customer.WhenNull(c => new Customer())
.Foo();
Customer customer = null;
if (customer == null)
{
customer = new Customer();
}
customer.Foo();
One developer sends me his version of the InNull 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 does not want to specify the base object in the lambda expression (in this case, “client”)
Customer customer = null;
customer.WhenNull(c => customer = new Customer())
.Foo();
I thought this could not be done.
Is it correct?
source
share