James Jones
James Jones

Reputation: 8773

C# Empty Statement

The C# language specification defines the empty-statement grammar production, which allows me to do something like this:

static void Main(string[] args)
{
    ; ; ;
}

Why would Microsoft include this grammar production in the C# language? Does it have a useful purpose?

Upvotes: 11

Views: 5932

Answers (13)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37050

Imagine you have some code that you want to extend by a new feature:

void DoSomething()
{
    // ...
    NewMethod();
}

Now you introduce a new member called NewMethod. However you don´t have the time to implement it appropriately and leave it empty:

public void NewMethod() { }

Some time later you can finally implement the code of that member without having to bother where and how to call it, because that was already done before.

You can also create an interface which you don´t implement. This way you only define what to do, but not how.

Another possible scenario is to have a virtual member that usually does nothing, but in a few cases (some specific implementation) you want the method to return a specific value. See this post for example: Virtual methods without body

Upvotes: 0

Eric Lippert
Eric Lippert

Reputation: 660367

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.

Upvotes: 12

Ed Power
Ed Power

Reputation: 8531

So some genius could write for(;;) instead of while(true). Like for an interview test.

Upvotes: 0

Péter Török
Péter Török

Reputation: 116286

C# inherits a lot from the C family, where you can do things like

for (i = 0; i < n; v[i++] = 1);

or

while (testSomeCondition());

That is, run a loop with an empty body where the "meat" is inside the brackets.

While one can debate about their merits and dangers, these are pretty common programming idioms in the C programming world, so this backward compatibility makes it easier for C/C++/Java programmers to learn C#.

Upvotes: 5

Simeon Pilgrim
Simeon Pilgrim

Reputation: 26043

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
;

Upvotes: 2

Chris Marisic
Chris Marisic

Reputation: 33108

This is an assumption but I would assume it goes to the base grammar for the compiler. Relating it to set theory the "empty set" is inside every single set. That the ; really is to define a set of lexical operations which always must define the ; which also would accept the base case which is empty statement.

Upvotes: 1

Asad
Asad

Reputation: 21928

For what it's worth, a good rule of thumb is that you should have stepped through in the debugger every line of code you write, at least once for each possible execution path

ref: Possible mistaken empty statement

Upvotes: 0

Andrey
Andrey

Reputation: 60085

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.

Upvotes: 1

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59503

while (GetWhitespace(textStream))
    ;

Upvotes: 14

Jim L
Jim L

Reputation: 2327

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?

Upvotes: 0

Sam Harwell
Sam Harwell

Reputation: 99949

Good practice or not, some people use this:

while (!SomeCondition)
    ; // <-- body of the while loop is an empty statement

Upvotes: 1

Andrey
Andrey

Reputation: 60085

if (b1)
   if (b2) else;
else
   //code

withoud this ; this will be painful. but this is not best way to write ifs also. but acceptable.

Upvotes: 0

jason
jason

Reputation: 241711

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.

Upvotes: 5

Related Questions