相关疑难解决方法(0)

Null-coalescing运算符和lambda表达式

看看我试图在构造函数中编写的以下代码:

private Predicate<string> _isValid;

//...

Predicate<string> isValid = //...;
this._isValid = isValid ?? s => true;
Run Code Online (Sandbox Code Playgroud)

代码不能编译 - 只是"无效的表达式术语"等等.

相比之下,编译,我可以使用它:

this._isValid = isValid ?? new Predicate<string>(s => true);
Run Code Online (Sandbox Code Playgroud)

但是,我仍然想知道为什么不允许这种语法.

有任何想法吗?

.net c# lambda null-coalescing-operator

9
推荐指数
1
解决办法
2763
查看次数

标签 统计

.net ×1

c# ×1

lambda ×1

null-coalescing-operator ×1