The easiest way is to just pull out async as well as lambdas here, as it emphasizes what is happening. Both of these methods are valid and will be compiled:
public static void Foo() { while (true) { } } public static Action Foo() { while (true) { } }
However, for these two methods:
public static void Foo() { while (true) { break; } } public static Action Foo() { while (true) { break; } }
The first compiler, and the second not. It has a code path that does not return a valid value.
In fact, while(true){} (along with throw new Exception(); ) is an interesting statement in that it is the actual body of a method with any return type.
Since an infinite loop is a suitable candidate for both overloads, and neither overload is "better", this leads to an ambiguity error. An implementation without an infinite loop has only one suitable candidate for overload resolution, so it compiles.
Of course, to bring async back to the game, this is really true in one case. For async methods, they both always return something, whether Task or Task<T> . The brilliance algorithms for resolving overloads will be preferable to delegates that return a value over void delegates when there is a lambda that can match, but in your case two overloads have delegates that return a value, the fact is that for async , return Task instead of Task<T> , is the conceptual equivalent of a non-returning value, is not included in this survival algorithm. Because of this, the non-asynchronous equivalent will not lead to an ambiguity error, even if both overloads are applicable.
Of course, it is worth noting that writing a program to determine whether an arbitrary block of code will ever be completed is, however, an insoluble problem, although the compiler cannot correctly evaluate whether each individual fragment is complete, it can prove, in certain simple cases such as this one that the code will never actually complete. Because of this, there are ways to write code that explicitly (for you and for me) never ends, but that the compiler will consider completion possible.