c# - why in this case the compiler doesn't complain? -
this code:
private treenode gettoplevelnode(treenode childnode) { if (childnode == null) throw new argumentnullexception("childnode", "childnode null."); if (childnode.parent == null) return childnode; treenode node = childnode; while (true) { if (node.parent == null) { return node; } node = node.parent; } }
in while loop, if node.parent == null, node returned,
why compiler doesn't report "not code paths return value" error?
if 'node.parent == null' can't satisfied , no tree node returned. compiler can't detect situation?
because using while(true){
, there no other way exit loop other using return. if node.parent == null
cannot satisfied, infinite loop. therefore, there no way past loop without returning, , compiler doesn't complain.
also, code specified return null treenode
, wanted?
edit: see fixed that.
Comments
Post a Comment