The C# language specification defines the empty-statement grammar production, which allows me to do something like this:
static void Main(string[] a
while (GetWhitespace(textStream))
;
Here are two uses:
while(DoSomething()) ;
and
void M() {
if(someCondition) goto exit;
// ...
exit: ;
}
There are many others. It's a useful device anytime there is nothing to do but a statement is required.
Why would Microsoft include this grammar production in the C# language?
the main answer to this question is: C# is C-like language and to make life of different developers easier MS decided to make most basic syntax elements compatible with other c-like languages. and it is good.
I personally would never use an empty statement. If I for some bizarre reason wanted to play code golf and write something like
while(Foo(x++)) ;
I'd be more inclined to use {} instead of ; as the "do nothing statement". I think that is more clear.
Pretty much it's there for historic reasons. There's nothing there that you cannot also do with {}.
UPDATE: I just thought of one possible usage case. In a debug build it allows you to put a breakpoint somewhere that you are guaranteed that you can break at which is not going to have any side effect when you step. If the code surrounding the breakpoint is particularly complicated it might be useful.
UPDATE UPDATE: I am WRONG WRONG WRONG. That doesn't work. It just moves the breakpoint to the next statement. How irksome.
Eric Lippert's a good person to ask.
Mostly, I suppose it's because it wouldn't cause any harm and it simplified their handling of the grammar. Also, why restrict people when you don't have to?
It allows have blocks of code IFDEF'd out, and the surrounding code still build. eg
if(true){
#if(DEBUG)
System.Console.WriteLine("Debug comment");
#endif
}
which could be written as an empty block, if you dislike brackets:
if(true)
#if(DEBUG)
System.Console.WriteLine("Debug comment")
#endif
;