-
I would expect the first method below to not compile, because the return type of the lambda does not match the signature of the delegate. However, it does compile. I've also explored the issue several other ways below that. class SomeClass{};
delegate void DoStuff();
public void ShouldNotCompileButDoesCompile()
{
DoStuff x = () => new SomeClass(); // issue is here
}
public void ShouldNotCompileAndDoesNotCompile()
{
DoStuff x = () => {return new SomeClass();}; // behaves as expected
}
public void ShouldCompileAndDoesCompile()
{
DoStuff x = () => {new SomeClass(); return;}; // behaves as expected
}
public void ShouldAlsoNotCompileAndDoesNotCompile_Struct()
{
DoStuff x = () => 4; // behaves as expected
}
public void ShouldAlsoNotCompileAndDoesNotCompile_ReferenceType()
{
var y = new SomeClass();
DoStuff x = () => y; // behaves as expected
} I think that the compiler is injecting a Is this compiling by design or by accident? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think the rule is that any expression that is allowed as an statement is allowed as the body of a void-returning lambda.
That's why you get the same error here. |
Beta Was this translation helpful? Give feedback.
I think the rule is that any expression that is allowed as an statement is allowed as the body of a void-returning lambda.
That's why you get the same error here.