The compiler knows that the second method will never return.
If either method ever returns in any circumstances then they must return a bool
.
The first method doesn't contain any infinite loops, doesn't throw any unconditional exceptions etc, so it must return a bool
. The code doesn't return a bool
so the compiler refuses to compile it.
The second method never returns because of the infinite while (true)
loop. If it never returns then it doesn't matter what (if anything) is never returned so the compiler will allow it to compile.
A few more examples that the compiler will recognise and allow:
public bool IsItTrue()
{
throw new Exception("Always thrown!");
}
public bool HowAboutThisOne()
{
if ((46 - 3) < (27 * 9))
{
throw new Exception("Always thrown!");
}
}