初始化底层对象有人问我,什么是错/如何能在以下情形可以固定扩展“WhenNull”检查null并通过拉姆达
Customer customer = null;
customer.WhenNull(c => new Customer())
.Foo();
// instead of
Customer customer = null;
if (customer == null)
{
customer = new Customer();
}
customer.Foo();
一位开发商向我WhenNull延长他的版本
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;
}
他的问题/意图是,他不希望指定lambda表达式的基本对象(在这种情况下,“客户”)
Customer customer = null;
customer.WhenNull(c => customer = new Customer())
.Foo();
我认为这是做不到的。
这是正确的吗?
我迷路了一下 - 你想达到哪一个?哪一个是错的? – Grzenio 2010-09-28 10:58:57