这是代码:为什么在这种情况下编译器不会抱怨?
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;
}
}
while循环,只有当node.Parent == null,则节点将被退回,
为什么编译器不会报告“并非所有的代码路径返回一个值“错误?
如果无法满足'node.Parent == null',则不会返回树节点。 编译器无法检测到这种情况?
我认为你的代码中存在一个bug:在while循环中:你不应该返回节点而不是node.Parent? – 2010-01-12 06:50:28
@Tommy,感谢您指出 – Benny 2010-01-12 06:54:23