[英]Why can't I use While(1) in C#?
I know this has probably already been answered somewhere, I just can't find it. 我知道这可能已经在某处得到了回答,我找不到它。 Why doesn't C# allow me to use
while(1)
? 为什么C#不允许我使用
while(1)
? I know that 'There is no conversion between the bool type and other types' in C# but why? 我知道'C#中的bool类型和其他类型之间没有转换',但为什么呢? What are the reasons for this, when in c++ it's perfectly acceptable.
这是什么原因,在c ++中它是完全可以接受的。
It is to keep programmers from accidentally doing something they did not want to do. 这是为了防止程序员意外地做一些他们不想做的事情。
Consider this common pitfall in C/C++: 考虑一下C / C ++中常见的陷阱:
int x = getValue();
if (x = 10) {
// do something
}
This will compile and run, but produce unexpected results (the programmer likely meant to check that x
equals 10
, not assign to x
-- otherwise, why would he need the test at all? It will always evaluate to true
). 这将编译并运行,但产生意想不到的结果(程序员可能意味着检查
x
等于10
,而不是分配给x
- 否则,为什么他需要测试?它总是评估为true
)。
By forcing conditionals to be of boolean type, you avoid this issue. 通过强制条件为布尔类型,可以避免此问题。
What are the reasons for this, when in c++ it's perfectly acceptable.
这是什么原因,在c ++中它是完全可以接受的。
Because C# isn't C++. 因为C#不是C ++。 The languages share a similar syntax but they're distinct.
这些语言具有相似的语法,但它们是截然不同的。 C# isn't a successor to C++, it's an entirely new language.
C#不是C ++的继承者,它是一种全新的语言。
I know that 'There is no conversion between the bool type and other types' in c# but why?
我知道'c#中的bool类型和其他类型之间没有转换',但为什么呢?
Why should there be? 为什么会这样? There's no logical conversion I can think of between an integer and a boolean value.
在整数和布尔值之间没有我能想到的逻辑转换。
Why doesn't c# allow me to use while(1)?
为什么c#不允许我使用while(1)?
Is there really something that wrong about using while(true)
? 使用
while(true)
真的有什么不对吗? =) On a slightly more serious note, while(...)
is basically saying, "whilst the condition described inside the brackets evaluates to true, perform the following action(s)". =)稍微更严重的说明,
while(...)
基本上是说,“虽然括号内描述的条件评估为真,但执行以下操作”。 There's really no logical way for the C# compiler to convert 1 to either true or false , hence it doesn't let you use it. C#编译器实际上没有逻辑方法将1转换为true或false ,因此它不允许您使用它。
You answered your own question: "There is no conversion between the bool type and other types." 你回答了自己的问题:“bool类型和其他类型之间没有转换。” The C# compiler does this to help enforce correctness and avoid common mistakes, like putting an assignment expression inside a while that always evaluates to true (using one equals sign instead of two).
C#编译器执行此操作是为了帮助强制执行正确性并避免常见错误,例如将赋值表达式放在一个总是求值为true的while中(使用一个等号而不是两个)。
if 0
is false
then which one should be true, 1
or -1
? 如果
0
为false
那么哪一个应该为真, 1
还是-1
? The first is what is assumed to be correct, but the second actully has all its bits set and is equal to ~0
(not false). 第一个是假设是正确的,但第二个actully的所有位都设置为等于
~0
(不是假)。
It forces a boolean evaluation. 它强制进行布尔评估。 Similarly, in order to protect assignment in an
if
statement's condition from providing a boolean value. 类似地,为了保护
if
语句的条件中的赋值不提供布尔值。
if (x = y)
this assignment will be evaluated in the sense that "did x get assigned y" - as opposed to "is x equal to y" - not what the programmer likely intended. 这个赋值将在“do x get assigned y”的意义上进行评估 - 而不是“x等于y” - 而不是程序员可能想要的。
If you want an infinitely looping condition us while(true)
; 如果你想要一个无限循环的条件我们
while(true)
; if you want to loop while a variable is a specific value, set a Bool to true prior to the loop: 如果要在变量是特定值时循环,请在循环之前将Bool设置为true:
Bool valid = (x == 42);
while(valid){//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.