Update
In C#10, this syntax is now valid and the compiler will infer a 'natural type' for a lambda Example here
C# 9 and Earlier
I am aware that Func<>
s cannot be implicitly typed directly via the var
keyword, although I was rather hoping that I could do the following assignment of a predicate:
Func<Something, bool> filter = (someBooleanExpressionHere)
? x => x.SomeProp < 5
: x => x.SomeProp >= 5;
However, I get the error cannot resolve the symbol, 'SomeProp'
At the moment, I have resorted to the more cumbersome if branch
assignment, which doesn't seem as elegant.
Func<Something, bool> filter;
if (someBooleanExpressionHere)
{
filter = x => x.SomeProp < 5;
}
else
{
filter = x => x.SomeProp >= 5;
}
Have I missed something, or will I need to stick with the if-branch assignment?
(x => x.SomeProp < 5)
– Outsole