空合并运算符和lambda EX pression [英] Null-coalescing operator and lambda expression

查看:166
本文介绍了空合并运算符和lambda EX pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一起来看看下面的code我试图在构造函数中写的:

 私人predicate<字符串> _已验证;

// ...

predicate<字符串>的isValid = // ...;
this._isValid =的isValid? S =>真正;
 

在code不编译 - 只是无效EX pression术语S等一个

在此相反,它编译,我可以只使用它:

  this._isValid =的isValid?新的predicate<字符串>(S =>真正的);
 

不过,我仍然不知道为什么这个语法是不允许的。

任何想法?

解决方案

  this._isValid =的isValid? (S =>真正的);
 

将工作:)

据分析是这样的:

  this._isValid =(参考isValid?S)=>真正;
 

这没有任何意义。

take a look at the following code I attempted to write inside a constructor:

private Predicate<string> _isValid;

//...

Predicate<string> isValid = //...;
this._isValid = isValid ?? s => true;

The code doesn't compile - just "invalid expression term"s and so one.

In contrast that does compile and I could just use it:

this._isValid = isValid ?? new Predicate<string>(s => true);

However, I still wonder why this syntax is not allowed.

Any ideas?

解决方案

this._isValid = isValid ?? (s => true);

Will work :)

It parsed it this way:

this._isValid = (isValid ?? s) => true;

which does not make any sense.

这篇关于空合并运算符和lambda EX pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆