views:

143

answers:

5

this is the code:

 private TreeNode GetTopLevelNode(TreeNode childNode)
    {
        if (childNode == null)
            throw new ArgumentNullException("childNode", "childNode is null.");

        if (childNode.Parent == null) return childNode;

        TreeNode node = childNode;
        while (true)
        {
            if (node.Parent == null)
            {
                return node;
            }
            node = node.Parent;
        }

    }

in the while loop, only if node.Parent == null, a node will be returned,

why the compiler doesn't report "not all code paths return a value" error?

if the 'node.Parent == null' can't be satisfied , then no tree node will be returned. The compiler can't detect this situation?

+10  A: 

Because you are using while(true){, there is no other way to exit the loop other than using the return. if node.parent == null cannot be satisfied, then it will be an infinite loop. Therefore, there is no way to get past the loop without returning, and the compiler doesn't complain.

Also, the code you specified would almost always return a null TreeNode, is that what you really wanted?

Edit: I see you fixed that.

zipcodeman
Smart compiler.
BlueRaja - Danny Pflughoeft
+1  A: 

The compiler is smart and optimises your loop. It knows that the only way out is to return after the if (node.Parent == null) condition returns true.

Jeremy McGee
+1  A: 

Unless you have an unlimited depth or a loop the while will continue forever.

So either the loop wil get stuck or at some point Parent will be null. It never enters after the while, as it does not end.

astander
+1  A: 

The compiler realizes that the function will return a TreeNode in all cases in which it will terminate. In all cases in which the function returns it will return a proper value. So there are no cases in which an undefined value would be returned and the compiler doesn't see a reason to warn.

If the function does not return there is no use in wondering about the return value.

sth
+8  A: 

Your question is actually one of the deepest and most interesting questions in computer science. This problem is known as the Halting Problem: the problem of, given a program, determine whether it always returns or runs forever.

The Halting Problem is famous because it is provably not solvable by computers. There is no algorithm which can reliably tell you whether a given program halts. You can prove that such a program either (1) gives wrong answers, (2) cannot analyze all programs or (3) itself sometimes never halts.

Therefore the C# compiler does not attempt to solve the Halting Problem. Rather, we simply detect that the "while(true)" and lack of breaks means that the loop is never left "out the bottom", and therefore the end point of the method is not reachable. What the error "not all code paths return a value" actually means is "there exists a code path which exits the method but does not return a value". It does not mean "there is a code path which runs forever" -- because figuring that out involves solving the Halting Problem.

Eric Lippert