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?
Will work :)
It parsed it this way:
which does not make any sense.
Check out this portion of the C# grammar:
Since
null-coalescing-expression
terminates withconditional-or-expression
thes
in your example will parse as asimple-name
. By wrapping it in parentheses it can then be parsed as aparenthesized-expression
.