哪个是正确的 C# 无限循环,for (;;) 还是 while (true)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1401159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which is the correct C# infinite loop, for (;;) or while (true)?
提问by Bob Kaufman
Back in my C/C++ days, coding an "infinite loop" as
回到我的 C/C++ 时代,将“无限循环”编码为
while (true)
felt more natural and seemed more obvious to me as opposed to
感觉更自然,对我来说似乎更明显,而不是
for (;;)
An encounter with PC-lintin the late 1980's and subsequent best practices discussions broke me of this habit. I have since coded the loops using the for
control statement. Today, for the first time in a long while, and perhaps my first need for an infinite loop as a C# developer, I am facing the same situation. Is one of them correct and the other not?
1980 年代后期与PC-lint的相遇以及随后的最佳实践讨论打破了我的这个习惯。我已经使用for
控制语句对循环进行了编码。今天,很长一段时间以来第一次,也许我作为 C# 开发人员第一次需要无限循环,我面临着同样的情况。其中一个正确,另一个不正确吗?
采纳答案by Adam Robinson
while(true)
{
}
Is always what I've used and what I've seen others use for a loop that has to be broken manually.
始终是我使用过的以及我看到其他人使用的必须手动中断的循环。
回答by Marplesoft
I think while (true)
is a bit more readable.
我认为while (true)
更具可读性。
回答by RSolberg
I think that this may be easier to read and is definitely the standard for use in C#:
我认为这可能更容易阅读并且绝对是 C# 中使用的标准:
while(true)
{
//Do My Loop Stuff
}
回答by Michael Petrotta
In those situations where I needed a true infinite loop, I've always used
在我需要真正无限循环的情况下,我一直使用
while(true) {...}
It seems to express intent better than an empty for statement.
它似乎比空的 for 语句更能表达意图。
回答by JRL
It should be while(true)
not while(1)
, so while(1)
is incorrect in C#, yes ;)
应该while(true)
不是while(1)
,所以while(1)
在 C# 中是不正确的,是的;)
回答by Chris
In terms of code readability while(true)
in whatever language I feel makes more sense. In terms of how the computer sees it there really shouldn't be any difference in today's society of very efficient compilers and interpreters.
就while(true)
我认为更有意义的任何语言的代码可读性而言。就计算机如何看待它而言,在当今非常高效的编译器和解释器的社会中,真的不应该有任何区别。
If there is any performance difference to be had I'm sure the translation to MSIL will optimise away. You could check that if you really wanted to.
如果有任何性能差异,我相信转换到 MSIL 会优化掉。如果你真的想要,你可以检查一下。
回答by Mark Synowiec
The only reason I'd say for(;;)
is due the CodeDom limitations (while loops can't be declared using CodeDom and for loops are seen as the more general form as an iteration loop).
我要说的唯一原因for(;;)
是 CodeDom 的限制(while 循环不能使用 CodeDom 声明,for 循环被视为迭代循环的更一般形式)。
This is a pretty loose reason to choose this other than the fact that the for
loop implementation can be used both for normal code and CodeDom generated code. That is, it can be more standard.
除了for
循环实现可用于普通代码和 CodeDom 生成的代码这一事实之外,这是一个非常松散的选择理由。也就是说,它可以更标准。
As a note, you can use code snippets to create a while
loop, but the whole loop would need to be a snippet...
请注意,您可以使用代码片段来创建一个while
循环,但整个循环需要是一个片段...
回答by Ben Blank
To rehash a couple of old jokes:
重述几个旧笑话:
- Don't use "
for (;;) {}
" — it makes the statement cry. - Unless, of course, you "
#define EVER ;;
".
- 不要使用“
for (;;) {}
”——它会使语句流泪。 - 当然,除非你“
#define EVER ;;
”。
回答by Pierre-Alain Vigeant
The C# compiler will transform both
C# 编译器将转换两者
for(;;)
{
// ...
}
and
和
while (true)
{
// ...
}
into
进入
{
:label
// ...
goto label;
}
The CIL for both is the same. Most people find while(true)
to be easier to read and understand. for(;;)
is rather cryptic.
两者的 CIL 相同。大多数人发现while(true)
更容易阅读和理解。for(;;)
比较神秘。
Source:
来源:
I messed a little more with .NET Reflector, and I compiled both loops with the "Optimize Code" on in Visual Studio.
我在.NET Reflector 上搞砸了一点,我在 Visual Studio 中使用“优化代码”编译了两个循环。
Both loops compile into (with .NET Reflector):
两个循环都编译为(使用 .NET Reflector):
Label_0000:
goto Label_0000;