什么是"用分号"空行;在C#是什么意思? [英] What does an "empty line with semicolon" mean in C#?
问题描述
我偶然发现了这个代码,并想知道为什么C#编译器提出任何警告或错误。严格地说,我想我试图执行什么这实际上是有效的吗? (为空行)
I stumbled upon this code, and was wondering why the C# compiler raises no warnings or errors. Strictly speaking, I guess I'm trying to execute nothing which is in fact valid? (for the empty lines)
推荐答案
这是一个空语句。这是一个循环体有用:(!条件())
It's an empty statement. It is useful as a loop body:
while(!Condition()) ;
在for循环,其中循环体的循环头完全嵌入多见。
More common in for-loops where the loop body is embedded fully in the loop header.
让我们遍历一个链表的最后一个元素:
Let's traverse to the last element of a linked list:
Node current = head;
for (; current.Next != null; current = current.Next) ;
return current;
这看起来有点讨厌,一般我宁愿写一篇较长,但更可读的循环来代替。 C ++的人往往死记硬背的东西进入循环头了不少。
It looks a little nasty and generally I prefer writing a longer but more readable loop instead. C++ people tend to cram stuff into the loop header a lot.
我敢肯定,它可以派上用场的代码生成的情况也是如此。
I'm sure it can come in handy in code-generation scenarios as well.
这篇关于什么是"用分号"空行;在C#是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!