Reputation: 4969
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)
Upvotes: 3
Views: 1461
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