c# - 条件运算符表达式(连续几个)

Dri*_*100 5 c# logic conditional-operator

bool isGeneric = variableA != null ? variableB != null ? false : true : true;
Run Code Online (Sandbox Code Playgroud)

嗨伙计们,我遇到过这条线.任何人都可以破译这条线/将它们分组到我的括号中吗?

感谢给予的任何帮助.提前致谢

Dav*_*d L 6

它是三元内部的三元组:

bool isGeneric = variableA != null 
    ? (variableB != null ? false : true) 
    : (true);
Run Code Online (Sandbox Code Playgroud)

如果variableA不等于null,请检查第一个条件,否则返回true.在第一个条件中,return falseif variableB不为null,true如果不是则返回.

您还可以将其翻译为以下if/else语句:

bool isGeneric = false;
if (variableA != null) 
{
    if (variableB != null)
        isGeneric = false;
    else 
        isGeneric = true;
}
else
    isGeneric = true;
Run Code Online (Sandbox Code Playgroud)