C#中的分号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/621933/
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
Semicolons in C#
提问by Loai Nagati
Why are semicolons necessary at the end of each line in C#? Why can't the complier just know where each line is ended?
为什么在 C# 中每行末尾都需要分号?为什么编译器不能只知道每一行的结束位置?
采纳答案by Mehrdad Afshari
The line terminator character will make you be able to break a statement across multiple lines.
行终止符将使您能够跨多行中断语句。
On the other hand, languages like VB have a line continuation character. I personally think it's much cleaner to terminate statements with a semicolon rather than continue using underscores.
另一方面,像 VB 这样的语言有一个换行符。我个人认为用分号终止语句比继续使用下划线要干净得多。
回答by WakeUpScreaming
So that whitespace isn't significant except inside identifiers and keywords and such.
因此,除了标识符和关键字等之外,空格并不重要。
回答by recursive
How many statements is this?
这是多少个说法?
for (int i = 0; i < 100; i++) // <--- should there be a semi-colon here?
Console.WriteLine("foo")
Semicolons are needed to eliminate ambiguity.
需要分号来消除歧义。
回答by TheSoftwareJedi
No, the compiler doesn't know that a line break is for statement termination, nor should it. It allows you to carry a statement to multilines if you like.
不,编译器不知道换行符用于语句终止,也不应该知道。如果您愿意,它允许您将语句带入多行。
See:
看:
string sql = @"SELECT foo
FROM bar
WHERE baz=42";
Or how about large method overloads:
或者大型方法重载如何:
CallMyMethod(thisIsSomethingForArgument1,
thisIsSomethingForArgument2,
thisIsSomethingForArgument2,
thisIsSomethingForArgument3,
thisIsSomethingForArgument4,
thisIsSomethingForArgument5,
thisIsSomethingForArgument6);
And the reverse, the semi-colon also allows multi-statement lines:
反之,分号也允许多语句行:
string s = ""; int i = 0;
回答by Konrad Rudolph
Strictly speaking, this is true: if a human could figure out where a statement ends, so can the compiler. This hasn't really caught on yet, and few languages implement anything of that kind. The next version of VB will probably be the first language to implement a proper handling of statements that require neither explicit termination nor line continuation [source]. This would allow code like this:
严格来说,这是真的:如果一个人能找出语句的结束位置,那么编译器也能。这还没有真正流行起来,很少有语言实现这种类型的东西。下一版本的 VB 可能是第一种实现正确处理既不需要显式终止也不需要行继续的语句的语言 [来源]。这将允许这样的代码:
Dim a = OneVeryLongExpression +
AnotherLongExpression
Dim b = 2 * a
Let's keep our fingers crossed.
让我们保持手指交叉。
On the other hand, this doesmake parsing much harder and can potentially result in poor error messages (see Haskell).
另一方面,这确实使解析变得更加困难,并且可能导致糟糕的错误消息(参见 Haskell)。
That said, the reason for C# to use a C-like syntax was probably due to marketing reasons more than anything else: people are already familiar with languages like C, C++ and Java. No need to introduce yet another syntax. This makes sense for a variety of reasons but it obviously inherits a lot of weaknesses from these languages.
也就是说,C# 使用类似 C 的语法的原因可能更多是出于营销原因:人们已经熟悉 C、C++ 和 Java 等语言。无需再介绍另一种语法。出于多种原因,这很有意义,但它显然继承了这些语言的许多弱点。
回答by Rad
I personally agree with having a distinct character as a line terminator. It makes it much easier for the compiler to figure out what you are trying to do.
我个人同意有一个独特的字符作为行终止符。它使编译器更容易弄清楚您要做什么。
And contrary to popular belief it is not possible 100% of the time to for the compiler to figure out where one statement end and another begins without assistance! There are edge cases where it is ambiguous whether it is a single statement or multiple statements spanning several lines.
与流行的看法相反,编译器不可能在没有帮助的情况下 100% 地找出一个语句在哪里结束,另一个语句在哪里开始!在某些边缘情况下,它是单个语句还是跨越多行的多个语句是不明确的。
Read this articlefrom Paul Vick, the technical lead of Visual Basic to see why its not as easy as it sounds.
阅读来自 Visual Basic 技术负责人 Paul Vick 的这篇文章,了解为什么它不像听起来那么容易。
回答by Dana
I've been mulling this question a bit and if I may take a guess at the motivations of the language designers:
我一直在思考这个问题,如果我可以猜测一下语言设计者的动机:
C# obviously has semicolons because of its heritage from C. I've been rereading the K&R book lately and it's pretty obvious that Dennis Ritchie really didn't want to force programmers to code the way he thought was best. The book is rife with comments like, "Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all" and in the section on functions they mention that they chose one of many format styles, it doesn't matter which one you pick, just be consistent.
C# 显然有分号,因为它继承了 C。我最近一直在重读 K&R 的书,很明显,Dennis Ritchie 真的不想强迫程序员按照他认为最好的方式进行编码。这本书充斥着这样的评论,“虽然我们对这个问题并不教条,但似乎应该很少使用 goto 语句,如果有的话”,并且在关于函数的部分中他们提到他们选择了许多格式样式中的一种,你选择哪一个并不重要,只要保持一致。
So the use of an explicit statement terminator allows the programmer to format their code however they like. For better or worse, it seems consistent with how C was originally designed: do it your way.
因此,显式语句终止符的使用允许程序员以他们喜欢的方式格式化他们的代码。不管好坏,它似乎与 C 最初的设计方式一致:按照自己的方式做。
回答by Michael Meadows
You could accurately argue that requiring a semicolon to terminate a statement is superfluous. It is technically possible to remove the semicolon from the C# language and still have it work. The problem is that it leaves room for misinterpretation by humans. I would argue that the necessity of semicolons is the disambiguation for the sake of humans, not the compiler. Without some form of statement delimitation, it is much harder for humans to interpret consise staements such as this:
您可以准确地争辩说,要求使用分号来终止语句是多余的。从技术上讲,可以从 C# 语言中删除分号并使其仍然有效。问题在于它为人类的误解留下了空间。我认为分号的必要性是为了人类而不是编译器消除歧义。如果没有某种形式的语句定界,人类就很难解释像这样的 consise 语句:
int i = someFlag ? 12 : 5 int j = i + 3
The compiler should be able to handle this just fine, but to a human the below looks much better
编译器应该能够很好地处理这个问题,但对人类来说,下面看起来好多了
int i = someFlag ? 12 : 5; int j = i + 3;
回答by akuhn
It can be done. What you refer to is called "semicolon insertion". JavaScript does it with much success, the reason why it is not applied in C# is up to its designers. Maybe they did not know about it, or feared it might cause confusion among programmers.
可以办到。您所指的称为“分号插入”。JavaScript 在这方面取得了很大的成功,它没有在 C# 中应用的原因取决于它的设计者。也许他们不知道,或者担心它会引起程序员之间的混乱。
For more details in semicolon insertion in JavaScript, please refer to the ECMA-script standard 262where JavaScript is specified.
有关 JavaScript 中分号插入的更多详细信息,请参阅指定 JavaScript的ECMA 脚本标准 262。
I quote from page 22 (in the PDF, page 34):
我引自第 22 页(在 PDF 中,第 34 页):
When, as the program is parsed from left to right, the end of the input stream of tokens is encountered and the parser is unable to parse the input token stream as a single complete ECMA Script Program, then a semicolon isa utomatically inserted at the end of the input stream.
When, as the program is parsed from left to right, a token is encountered that is allowed by some production of the grammar, but the production is a restricted production and the token would be the first token for a terminal or nonterminal immediately following the annotation “[no LineTerminator here]” with in the restricted production (and there fore such a token is called a restricted token), and the restricted token is separated fromt he previous token by at least one LineTerminator, then a semicolon is automatically inserted before the restricted token.
However, there is an additional overriding condition on the preceding rules: a semicolon is never inserted automatically if the semicolon would then be parsed as an empty statement or if that semicolon would become one of the two semicolons in the header of a for statement (section 12.6.3).
[...]
当程序从左到右解析时,遇到令牌输入流的末尾并且解析器无法将输入令牌流解析为单个完整的 ECMA 脚本程序时,会在末尾自动插入分号的输入流。
当程序从左到右解析时,遇到语法的某些产生式允许的标记,但该产生式是受限产生式并且该标记将是紧跟注释之后的终结符或非终结符的第一个标记“[no LineTerminator here]”在受限产生式中(因此这种标记称为受限标记),并且受限标记与前一个标记之间至少有一个 LineTerminator 隔开,然后在受限制的令牌。
但是,前面的规则还有一个额外的覆盖条件:如果分号随后将被解析为空语句,或者该分号将成为 for 语句(部分12.6.3)。
[...]
The specification document even contains examples!
规范文档甚至包含示例!
回答by Lara Dougan
I would say that the biggest reason that semicolons are necessary after each statement is familiarity for programmers already familiar with C, C++, and/or Java. C# inherits many syntactical choices from those languages and is not simply named similarly to them. Semicolon-terminated statements is just one of the many syntax choices borrowed from those languages.
我想说,在每个语句之后都需要分号的最大原因是已经熟悉 C、C++ 和/或 Java 的程序员熟悉。C# 从这些语言中继承了许多句法选择,而不是简单地与它们相似地命名。分号终止的语句只是从这些语言中借用的众多语法选择之一。