在C#是为(;;)安全?它真的?安全

由网友(不尽人意)分享简介:我发现了一个空的for语句在code现有的一点,我不知道它做什么,是它的安全。它只是感觉错了。I found an empty for statement in an existing bit of code and I'm wondering what it does and is it "safe". It ju...

我发现了一个空的for语句在code现有的一点,我不知道它做什么,是它的安全。它只是感觉错了。

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 statements and a case statement
}

谢谢!

推荐答案

这是创造一个无限循环的一种方式。这是一个普通的循环,但空的初始化,条件和增量EX pressions。由于条件EX pression是一个空操作,该循环永远不会退出。这是完全安全的假设它有一个终止条件(A 破发返回语句[甚至 GOTO ,我想])的地方。

This is one way of creating an infinite loop. It's a regular for loop, but with empty initialization, condition, and increment expressions. Because the condition expression is a no-op, the loop never exits. It's perfectly "safe" assuming it has a terminating condition (a break or return statement [or even a goto, I suppose]) somewhere.

我个人preFER写与无限循环,而 S:

Personally, I prefer to write infinite loops with whiles:

while (true) 
{  
    //some statements and a case statement
}

(因为是迭代和,而是重复的)。

不过,看完this问题(由@jailf链接),我现在preFER ,而(42){...}

However, after reading this question (linked by @jailf), I now prefer while (42) { ... }.

阅读全文

相关推荐

最新文章