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

c# - conditional operator expression (a few in a row)

bool isGeneric = variableA != null ? variableB != null ? false : true : true;

我遇到了这条线。谁能帮我把这个 line/group 解释成括号?

是三元中的三元:

bool isGeneric = variableA != null 
    ? (variableB != null ? false : true) 
    : (true);

如果variableA不等于null,检查第一个条件,否则return为真。在第一个条件中,return false 如果 variableB 不为空,return true 如果为空。

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

bool isGeneric = false;
if (variableA != null) 
{
    if (variableB != null)
        isGeneric = false;
    else 
        isGeneric = true;
}
else
    isGeneric = true;