A; without operator in c #

I was looking for sample code in C #. There is ; without any statement to him. I thought it was a typo. I tried to compile with ; . It is well composed. What is the use ; without any code instruction?

I am 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); } } 
+6
source share
6 answers

The semicolon in C # is just the notation for the end-operator. Empty statements or simple ; valid per se.

You can have the following on the line inside any function in C #, and it should compile fine:

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

In the same topic, but semi-different from the issue at hand, is an empty set of curly braces, { } . They mean "code block", but are valid anywhere in your code. Again, you can have something like the following on a single line, and it will still compile fine:

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

In the end, an empty statement and empty codes block all compilations to "nothing to see people here, move" and in most cases can be removed from the code without consequences.

+13
source

As a C # developer, I use the "empty statement"

 ; 

(useful case in the requested comment)

when I have a multi-line lambda and I want to examine the last line of the evaluation ie

 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 a point inside some code after evaluating the line before ie

 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 } 
+7
source

It is a statement that does nothing. Usually this would be pointless and could just be deleted, but there are times when approval is expected and you really don't want anything.

Sometimes you see this with cycles that cause side effects and therefore do not need a body:

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

Personally, I donโ€™t like such code examples and impede practice, since they are usually more difficult to understand than loops that have free side effects. Using a set of empty curly brackets is clearer as it includes the corresponding comment, for example:

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

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

 for(;;) { //stuff } 

essentially coincides with:

 while(true) { //stuff } 
+6
source

This is an empty statement . I have never used it, but it exists in many languages.

+3
source

A semicolon (;) indicated the end of the statement. so if you just add a semicolon without anything ... that means it's an empty element

+2
source

An empty expression is sometimes used when an operator expects a block, but you do not want it to do anything.

For instance:

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

or for nested ones, if then elses without curly 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 not ! that. ; // do nothing else { ohnoes(); } 

But in your example, it does nothing when creating for release and just adds nop when building for debugging, so you can reset a breakpoint on it.

+1
source

All Articles