我有两个参数的方法:bool1和bool2.两者都是布尔值.我必须在代码中处理这些条件的每个组合.有没有更好的方法来使用嵌套的if/else:
if (bool1)
{
if(bool2)
{
}
else
{
}
}
else
{
if(bool2)
{
}
else
{
}
}
Run Code Online (Sandbox Code Playgroud)
var bothAreTrue = bool1 && bool2;
if(bothAreTrue){
}else if(bool1){
}else if(bool2){
}else{ //none is true
}
Run Code Online (Sandbox Code Playgroud)
if (bool1 && bool2) { }
else if (bool1) {}
else if (bool2) {}
else {}
Run Code Online (Sandbox Code Playgroud)