简体   繁体   中英

How do you make an 'if' statement do "nothing"?

Here is an example of my code. I need the last if to do nothing, else do something.

if (daPilot.Gas > 0)
    ;
else
    daPilot.failMessage3();

You've already done it. Congratulations.

Of course the far less confusing design is to just NOT the condition and then have an if with no else .

if (daPilot.Gas > 0) 
{
    // nothing
}                        
else
{
    daPilot.failMessage3();
}

Or more simply,

if (daPilot.Gas <= 0) 
{        
    daPilot.failMessage3();
}

Two ways:

  1. Empty curlys:

     if (condition) { } else { method(); }
  2. Realize that you can simply invert the conditional:

     if (!condition) method();

Simply invert the if condition and declare only the true part:

if (daPilot.Gas <= 0)
    daPilot.failMessage3();

Anyway, you are always able to declare an empty body when needed: { } .
For instance:

while (condition)
{
}

There is no point of putting a condition for doing nothing. You should do it like this.

if (daPilot.Gas <= 0)
    daPilot.failMessage3();

反转if

if (daPilot.Gas <= 0) daPilot.failMessage3();

You can invert if , try this:

if (daPilot.Gas <= 0)
{
    daPilot.failMessage3();
}

if you really want it to do nothing

       {
            if (daPilot.Gas > 0)
            {
            }
            else
            {
              daPilot.failMessage3();
            }
        }

Let's use the '!' operator like:

if (!(daPilot.Gas > 0))
      daPilot.failMessage3();

or with an extra variable:

var isGasGreaterToZero = daPilot.Gas > 0; /*true or false*/

if(!isGasGreaterToZero)
    daPilot.failMessage3();

In both cases if daPilot.Gas is greater than zero (0) nothing will happen!

I like the following code - it uses zero or one line but need extend to tree lines

#pragma warning disable CS0642 // Possible mistaken empty statement
                ;
#pragma warning restore CS0642 // Possible mistaken empty statement

Also use { } in two lines or zero line in if statement:

if (...) { }

Inside the if statement write some code that does nothing:

if (daPilot.Gas > 0) var S = true;

I concur with the other answers here. Better to reverse the condition and if you dont want to do that, better to use the empty block { /* nothing */ } .

But now, suppose you are looking for a solution that:

  1. Does not reverse the condition.
  2. Does not use code blocks.
  3. Does not generate a warning or an error or suppress such message.
  4. Has minimal impact on generated compiler code.

How can you do that?

Answer: use _ = 0 .

I came up with the following code example which is valid in .NET 3 and 5:

if (new Random().Next(2) == 0)
    _ = 0; // do nothing
else
    Console.WriteLine("Do Something");

If you ckeck at the compiler generated IL code without optimization you see the the instruction has been replaced by a nop .

在此处输入图片说明

With optimizations, the condition has been reversed by the compiler and the then part of the conditional is completely optimized away.

在此处输入图片说明

Conclusion: _ = 0; fits the bill.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM