开发者

Why doesn't ; ; result in a build error in VS?

No matter how much ; you placed at the end of a C# code line, the compiler will not show an error and the build is success开发者_开发百科ful.

In almost all other languages like C, C++ and Java. This is not allowed.


Your contention that this pattern is illegal in C, C++ and Java is completely false.

I refer you to:

The C Programming Language, 2nd edition, section A9.2:

... the construction is called a null statement; it is often used to supply an empty body to an iteration statement...

The C++ Programming Language, 2nd edition, section r.6.2

An expression statement with the expression missing is called a null statement; it is useful ... to supply a null body to an iteration statement ...

The Java Language Specification, 1st edition, section 14.5

An empty statement does nothing.

The C# Language Specification, 4th edition, section 8.3:

An empty statement is used when there are no operations to perform in a context where a statement is required.


The empty statement:

http://msdn.microsoft.com/en-us/library/aa664739(v=vs.71).aspx

No matter how many you have - still does nothing....

You can do the same thing in C/C++, and probably Java too:

Why are empty expressions legal in C/C++?


Interestingly related to Eric's blog on why is this not a warning.

From Eric Lippert's blog " I am often asked why a particular hunk of bad-smelling code does not produce a compiler warning."

http://blogs.msdn.com/b/ericlippert/archive/2011/03/03/danger-will-robinson.aspx

The point being, would it be good use of the compiler teams valuable time to introduce such a warning?


Why not? ; delimits statements in many forms of code flow. A single ; by itself simply means "nothing happens here". Putting a bunch of ;s together still results in, well, nothing happening!


; is an empty statement, and it's perfectly legitimate. What's your objection to having a series of consecutive ;'s? :-)


Adding multiple ; means you add empty statements. They are legal.


Empty statements (a semicolon with nothing in front of it) are allowed in both C, C++, C#, and Java.


It results in blank commands, that's all. Probably Microsoft decided to let their shiny language shoot blanks, if that's what the programmer wants. :)

The complier most probably is removing this/ignoring it altogether as part of optimization.


Also, consider languages where newlines or whitespace is used to mark the end of a code block instead of semi-colons. In these languages it's not an error to leave blank lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜