Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
764 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

Will work :)

It parsed it this way:

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

which does not make any sense.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...