简体   繁体   English

如果条件是csharp,最好的方法是什么?

[英]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: 但是由于De Morgan的法律,我们可以这样重写它:

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 A = 1 || B = 1 same as A != 1 && B != 1 , so; A = 1 || B = 1A != 1 && B != 1 ,所以;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Linq to NHibernate中测试状况的最佳方法是什么? - What is the best way to test a condition in Linq to NHibernate? 什么是在Windows窗体中进行验证的最佳方法 - What is the best way to do validations in windows forms .Net深度克隆 - 最好的方法是什么? - .Net Deep cloning - what is the best way to do that? 在NHibernate中进行热切联接的最佳方法是什么? - What is the best way to do eager joins in NHibernate? protobuf-csharp-port这些工具最适合今天使用什么版本? - protobuf-csharp-port what version(s) of these tools are best to use today? 在javascript中执行split()并忽略空白条目的最佳方法是什么? - What is the best way to do split() in javascript and ignore blank entries? C#,LINQ批处理项目-最好的方法是什么? - C#, LINQ batch items - what is the best way to do this? 在.NET中使用返回值执行多线程或异步任务的最佳方法是什么? - What is the best way to do multithreading or asynchronous task in .NET with return value? 循环图像旋转在WPF中最好的方法是什么 - round robin image rotation what is the best way to do in WPF 在xslt中进行替换的最佳(更快)方法是什么 - What is the best(more fast) way to do replace in xslt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM