gmail user
gmail user

Reputation: 2783

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);
     }
  }

Upvotes: 6

Views: 569

Answers (6)

Paul Sullivan
Paul Sullivan

Reputation: 2875

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
}

Upvotes: 7

Hafthor
Hafthor

Reputation: 16906

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.

Upvotes: 1

Dan Hunex
Dan Hunex

Reputation: 5318

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

Upvotes: 2

newfurniturey
newfurniturey

Reputation: 38456

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.

Upvotes: 13

mrab
mrab

Reputation: 2728

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

Upvotes: 3

Servy
Servy

Reputation: 203847

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
}

Upvotes: 6

Related Questions