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

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -