简体   繁体   中英

What is the best way to do if condition in csharp?

I have the following code that works:

if (user.ReAccess == 1 || user.CetAccess == 1)
        {
        }
        else
        {
            //Do Something
        }

But, ideally I would like to do something like this (if not). But this has syntax error.

if !(user.ReAccess == 1 || user.CetAccess == 1)
        {
    //Do Something
        }

The most direct way would be to do this:

if (!(user.ReAccess == 1 || user.CetAccess == 1))

But thanks to De Morgan's law, we could rewrite it like this:

if (user.ReAccess != 1 && user.CetAccess != 1)

Surround it with braces:

if (!(user.ReAccess == 1 || user.CetAccess == 1))
{
    //Do Something
}

Use not equal to operator

 if (user.ReAccess != 1 && user.CetAccess != 1)
    {
        //Do Something
    }

Try...

if (!(user.ReAccess == 1 || user.CetAccess == 1))
    {
//Do Something
    }

Use DeMorgans. If A = 1 || B = 1 A = 1 || B = 1 same as A != 1 && B != 1 , so;

if (user.ReAccess != 1 && user.CetAccess 1= 1)
   {
      // Do Something
   }

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