In C# is a for(;;) safe and what does it really do?

后端 未结 10 1169
迷失自我
迷失自我 2021-01-17 12:59

I found an empty for statement in an existing bit of code and I\'m wondering what it does and is it \"safe\". It just feels wrong.

for(;;)
{
   //some if sta         


        
10条回答
  •  遥遥无期
    2021-01-17 13:39

    It's valid syntax for an infinite loop. You need to "break;" out of it. This was popular back in the C++ days IIRC.

    As far as being safe, you're right in feeling "wrong" about this. Usually there would be an "if" condition inside where you would decide if you continue or break the loop. If you don't verify all execution paths it could very well lead to an infinite loop. I would try and do this some other way.

提交回复
热议问题