Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a ; without statement in C#

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);
     }
  }
like image 216
gmail user Avatar asked Jan 08 '13 16:01

gmail user


4 Answers

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.

like image 114
newfurniturey Avatar answered Sep 22 '22 14:09

newfurniturey


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
}
like image 40
Paul Sullivan Avatar answered Sep 23 '22 14:09

Paul Sullivan


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
}
like image 27
Servy Avatar answered Sep 22 '22 14:09

Servy


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

like image 43
mrab Avatar answered Sep 22 '22 14:09

mrab