Caught this confusing error today.
I tried to compile a very similar example today, where it told me the return type is void even though I’m trying to use the Func
overload, despite clearly having return
statements in my lambda. I thought this error was weird because the required return type is Animal
, which both Dog
and Cat
are. I’m returning objects in the lambda, so it’s obviously a Func
. However, both R# and the compiler can’t figure out that Dog
and Cat
are Animal
s and chooses the Action
overload and yells at me for trying to return.
ReSharper’s quick-fix context menu also has no idea how to solve the issue. The issue is resolved by casting any of the return statements to Animal
. The compiler isn’t confused at all if there is only one return statement (with no cast).