scatman
scatman

Reputation: 14555

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 successful.

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

Upvotes: 0

Views: 252

Answers (9)

Adam Straughan
Adam Straughan

Reputation: 2848

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?

Upvotes: 1

Eric Lippert
Eric Lippert

Reputation: 660347

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.

Upvotes: 9

cdlk
cdlk

Reputation: 546

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.

Upvotes: 0

James Gaunt
James Gaunt

Reputation: 14783

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++?

Upvotes: 6

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

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.

Upvotes: 0

Sasha Goldshtein
Sasha Goldshtein

Reputation: 3519

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

Upvotes: 1

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37960

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

Upvotes: 0

Cosmin
Cosmin

Reputation: 2385

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

Upvotes: 0

BoltClock
BoltClock

Reputation: 724212

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!

Upvotes: 1

Related Questions