Consider this snippet of code:
Func<int, bool> TestGreaterThanOne = delegate(int a) {
if (a > 1) return (true);
else return(false);
};
In the above code, I cannot delete the “else return(false)” statement – the compiler warns that not all code paths return a value. But in the following code, which uses a lambda…
Func<int, bool> TestGreaterThanOne = a => a > 1;
I do not have to have an “else” statement – there are no compiler warnings and the logic works as expected.
What mechanism is at play here to make me not have an “else” statement in my lambda?
Because in your lambda shorthand, there is no if statement either. Your lambda shorthand is equivalent to
Therefore all code paths return a value.