[英]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 = 1
与A != 1 && B != 1
,所以;
if (user.ReAccess != 1 && user.CetAccess 1= 1)
{
// Do Something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.