views:

173

answers:

3

In C#, the following method will not compile:

public bool IsItTrue()
{
}

The compiler errors : 'IsItTrue()': not all code paths return a value, which makes perfect sense. But the following compile without any issue.

public bool IsItTrue()
{
    while (true)
    {
    }
}

Which looks wrong as no return statement at all. Why is it so? Any help here...,

+13  A: 

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!");
    }
}
LukeH
Little explanation please..,
Dhana
Interesting that the compiler would allow that. I can see why it *could* be allowed, but I wonder what real-life purpose that serves.
Eric Petroelje
I know that we might want something similar to a while(true) for a thread but even a thread should end at some point? So if the compiler can detect such a situation why does it not throw an error point an infinite loop?
uriDium
This works because the loop or condition expression can be evaluated at compile time. See my answer in this thread: http://stackoverflow.com/questions/1952634/why-compiler-behaves-differently-with-this-code/1953368#1953368
Steve Guidi
+3  A: 

The first is explained nicely by the compiler error message.

The second never returns, so there's never any value returned.

It's not the same. In your first example the method could return without giving any value back to the caller -> Compiler error.

The second will never return (the compiler is smart enough for that, it figures out that you created an infinite loop). It won't ever enter the "Okay, I reached the end of the method and don't know what to return" state.

Benjamin Podszun
+1  A: 

The Halting Problem states that you can not generally determine if a program will terminate or run forever. Given that there are examples in this thread that seem to violate this principle, I suspect that the C# compiler is performing analysis on the loop conditions that can be reduced to a compile time constant. If the constant evaluates to true, then we know that the loop will never terminate.

For example, consider the following two functions.

public bool NoError()
{
    while (true) { }
}

public bool Error()
{
    while (NoError()) { }
}

As demonstrated, the first function will not generate a compile time error. However, the second will since the compiler can not evaluate the result of the function call NoError(). This is also the case if NoError() is modified to always return true.

Steve Guidi