繁体   English   中英

多个2值的有效方法true if语句

[英]Efficient way for multiple 2 value true false if statement

我有一个if else语句,该语句具有2个值,需要评估它们是否为null,然后基于该值选择正确的语句。 下面的代码:

int? x;
int? y;

if(x == null and y == null) { do this part; }
else if (x != null and y == null) {do this second part; }
else if (x == null and y != null) {do this third part; }
else { do this last part; } 

我试图找到是否有一种更有效的方法来实现这一目标。 可以使用案例声明,但是我仍然想知道是否有更好的方法。

我将使用嵌套的if ,因此每个变量仅求值一次,而不是如OP片段中建议的那样求值多次。

if (x == null) {
    if (y == null) {
        // Both are null
    } else {
        // Only x is null
    }
} else {
    if (y == null) {
        // Only y is null
    } else {
        // Neither are null
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM