why in this case the compiler doesn't complain?

c#, compiler-errors

Solution

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.

Problem

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?

Original source