6

I was looking at a code sample in C#. There is ; without any statement before it. I thought it is typo. I tried to compile with ;. It compiled fine. What is the use of ; without any code statement?

I'm using VS 2010, C# and .Net 4.0

  private void CheckSmcOverride(PatLiverSmc smc)
  {
     ;
     if (smc.SmcOverride && smc.Smc != null 
              && smc.Smc.Value < LiverSmcConst.SMC_OVERRIDE_POINT)
     {
          smc.Smc = 10;
          _logger.DebugFormat("CheckSmcOverride: Override SMC {0}", smc.Smc);
     }
  }
4
  • 1
    Does it compile without the semicolon as well? Jan 8, 2013 at 16:44
  • It is just an empty statement. It is valid and making it invalid would cost in resources for little benefit.
    – Oded
    Jan 8, 2013 at 16:45
  • It means nothing. It's an empty statement. Try removing it and recompiling.
    – mpen
    Jan 8, 2013 at 16:46
  • My code compiles just fine with semi colon with no statement too.
    – Anonymous
    Jan 8, 2013 at 16:47

6 Answers 6

13

A semicolon in C# is simply to denote an end-of-a-statement. Empty statements, or just a ; by itself, are valid.

You could have the following on a line by itself inside any function in C# and it should will compile fine:

; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;

On the same topic, but semi-different from the question at hand, is an empty set of curly-brackets, { }. These denote a "code block", but are valid just about anywhere in your code. Again, you could have something like the following on a single line and it will still compile fine:

{ } { ;;;;;;;;;; } { }

In the end, the empty-statement and empty-code blocks all compile down to "nothing to see here folks, move along" and can, in most cases, be removed from the code without consequence.

2
  • I've seen a question the other day (or a comment in C# chat, not sure which) in which the IL generated from an empty set of curly-brackets generated one single additional IL instruction. If I had a reference to where I saw it, I'd link it here... Jan 8, 2013 at 17:38
  • @ShotgunNinja so are you saying that using if(something); versus if(something){}; saves one byte of IL? (I did check that ; didn't generate a IL NOP which it doesn't) Jan 8, 2013 at 17:53
7

As a c# developer I use the 'empty statement'

;

(a useful case as a comment requested)

when I have a multi line lambda and I want to examine the last line of evaluation i.e.

list.ForEach(x=>
              {
                x.result = x.Value * x.AnotherValue;
                ; // otherwise I can't ever see these as you can't break on the end brace of an anonymous function
              })

as a way to break point inside some code after evaluation of the line before i.e.

void SomeFunct()
{
    int a = someOtherFunct();
    ; //I want a breakpoint here but...
    //there is some huge code segment that will get skipped before I can breakpoint
}
3
  • 2
    Your use of it does not equal why it is allowed ;)
    – RhysW
    Jan 8, 2013 at 16:48
  • @ispiro nearly all of them, the question is what is its use with no code statement. it has more uses than just wanting a breakpoint, Servy provides an excellent and comprehensive description of even more uses just to name one
    – RhysW
    Jan 8, 2013 at 17:03
  • @RhysW added lambda breakpoint example (a regular use for empty statement break point for me) Jan 8, 2013 at 17:43
6

It's a statement that does nothing. Normally this would be pointless and could just be removed, but there are times where a statement is expected and you really want nothing to happen.

Sometimes you see this with loops that cause side effects and so need no body:

int count = 0;
while(isTheRightNumber(count++))
    ;

Personally I dislike such code examples and discourage the practice as they tend to be harder to understand than loops that have side effect free conditions. Using a set of empty braces is a bit clearer, as is including a relevant comment, such as:

int count = 0;
while(isTheRightNumber(count++)) 
    { } //empty by design

Another example is the pattern of using a for loop for an infinite loop:

for(;;)
{
    //stuff
}

is essentially the same as:

while(true)
{
    //stuff
}
5
  • On the topic of the For loop, im sure its the fact the paramaters are empty that make it infinite not the fact it has semi colons, as those are always there when you give parameters too. the semicolons themselves dont make it infinite, the lack of parameters do
    – RhysW
    Jan 8, 2013 at 16:56
  • @RhysW The point is that for() wouldn't compile, you need to have 3 separate statements inside the declaration of the loop, but you don't need them to do anything; you effectively want 3 empty statements, hence the use of a statement that is just ;.
    – Servy
    Jan 8, 2013 at 16:59
  • perhaps a little bit of explanation would go nicely with that then? nothing wrong with providing the curious with more relevant information!
    – RhysW
    Jan 8, 2013 at 17:00
  • @RhysW The info is in the comments if anyone is interested; personally I don't use the pattern and find while(true) to be much more readable, it seems overly verbose to expand on it in the answer.
    – Servy
    Jan 8, 2013 at 17:02
  • i meant more to denote that the reason it is infinite is because youre supplying three empty statements,
    – RhysW
    Jan 8, 2013 at 17:04
3

It's an empty statement. I never used it, but it exists in many languages.

2

semicolon(;) indicated the end of a statement. so if you just add a semicolon without anything... it means it is empty statment

1

An empty statement is sometimes used when a statement expects a block but you don't want it to do anything.

For example:

for(i=0; array[i]!=null; i++)
    ;

or for nested if then elses without braces:

// don't really do this kids
if(cond1)
  if(cond2)
    doit();
  else
    ;
else
  dont();

Sometimes used for 'if' clarity:

if(somecomplicatedconditionisnotfalseinverted()) // <-- this is already complicated enough, let's not ! that.
  ; // do nothing
else {
  ohnoes();
}

But in your example, it does absolutely nothing when built for release and just adds a nop when built for debug, so you can drop a breakpoint on it.

2
  • 2
    your dangling else case would be much more effectively solved with the use of braces. I've never actually seen that solution before, but I know I wouldn't want to see it in production code.
    – Servy
    Jan 8, 2013 at 16:52
  • As for your edit, the addition of a noop if and an additional else is more complex than a ! in my opinion. If the expression is overly complex it should probably be broken up into sub expressions that result in intermediate variables being stored before the if, or possibly a bool returning method (like the code you have there), if it's really that complex.
    – Servy
    Jan 8, 2013 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.