处理两种bool条件组合的更好方法是什么?

Kam*_*ski 1 c# if-statement

我有两个参数的方法:bool1和bool2.两者都是布尔值.我必须在代码中处理这些条件的每个组合.有没有更好的方法来使用嵌套的if/else:

if (bool1)
{
    if(bool2)
    {
    }
    else
    {
    }
}
else
{
    if(bool2)
    {
    }
    else
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

ced*_*lof 6

var bothAreTrue = bool1 && bool2;

if(bothAreTrue){

}else if(bool1){

}else if(bool2){

}else{ //none is true

}
Run Code Online (Sandbox Code Playgroud)


Neo*_*Neo 6

if (bool1 && bool2) { }
else if (bool1) {}
else if (bool2) {}
else {}
Run Code Online (Sandbox Code Playgroud)

  • 在验证码中延迟:) (2认同)