wonea
wonea

Reputation: 4969

What does an "empty line with semicolon" mean in 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)

semi colons on blank lines

Upvotes: 3

Views: 1461

Answers (1)

usr
usr

Reputation: 171206

It's an empty statement. It is useful as a loop body:

while(!Condition()) ;

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;

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.

Upvotes: 5

Related Questions