Alex
Alex

Reputation: 3968

What does a semi colon do after a conditional block in C#?

I recently came across this code in a project - which I assume was there by mistake:

if(condition)
{
   //Whatever...
};

Note the semi colon after the closing brace.

Does anyone know what the effect of this is?

I assume it does not have any effect, but would have thought it would have caused a compiler error.

Upvotes: 32

Views: 4112

Answers (3)

scharette
scharette

Reputation: 9997

This is a simple question with a simple answer, but I just wanted to add something relevant. Often people understand that it does nothing and particularly for the case that you presented, the semi-colon is an unnecessary line termination.

But what is the rationale behind it ?

Actually, those empty statements are allowed for statement like these:

 // Use an empty statement as the body of the while-loop.
while (Method())
        ;

I agree that it does nothing. But it can help certain loops conform to the syntactic requirements of the language and I think this is what people should understand from it. As other said, I agree you can remove it, I just wanted to underline why C# allows it.

Further clarification

An empty statement is used when you don't need to perform an operation where a statement is required. It simply transfers control to the end point of the statement. It has no effect at all, it is pure syntactic sugar.

As stated by @PaulF, in the example above, you could use an empty block ({}) instead. It would be totally valid and have the same effect.

Again, it all comes down to style. You don't need it, but it can certainly help you conform to whatever rules of your coding environments.

Common use-cases (where one could see empty statements)

  • While loop with empty body (same case that I underlined above)

    void ProcessMessages()
    {
        while (ProcessMessage())
            ; // Statement needed here.
    }
    
  • goto statements (rarely use but still valid)

    void F()
    {
        //...
        if (done) goto exit;
    //...
    exit:
        ; // Statement needed here.
    }
    

    From MSDN

  • Class declaration (Props to @EricLippert for bringing this one)

    class SomeClass
    {
        ...
    };
    

Note that in this case, as stated by @EricLippert in the comments section, this is simply a courtesy to C++ programmers who are used to typing semis after classes; C++ requires this.

Even though the general use of empty statements is debatable mainly because of the confusion they can bring, in my opinion, syntactically speaking they have a place in C#. We must not forget that C# is an increment of C++ (which mostly explain the # aka. four "+" symbols in a two-by-two grid) and for historical reasons, allowing empty statements was facilitating the transition.

Upvotes: 35

Fuzzybear
Fuzzybear

Reputation: 1418

That is something that Visual Studio will compile as valid syntax for an empty statement, as it is just a statement termination. Your code will compile and the extra ; will not be an issue.

It can be deleted to clean up the code if you want to, but leaving it in will not cause any adverse effect.

Hope this helps.

Upvotes: 8

Isaac Abramowitz
Isaac Abramowitz

Reputation: 540

It doesn't seem to have any effect, though I wouldn't recommend writing code that way.

In the event that you ever want to add an else or else if after the ;, it won't compile.

Ex:

if(5>1) {
  //whatever
}; else {
  //whatever
}

This will not compile (note the ; before else)

Upvotes: 17

Related Questions