C # - conditional statement expression (multiple lines)

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

Hi guys, I came across this line. Can someone decrypt this line / group them in brackets for me?

Appreciate any help provided. Thanks in advance: D

+5
source share
1 answer

This is a triple inside a triple:

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

If variableA not null, check the first condition, else will return true. In the first condition, return false if variableB not null and returns true if it is.

You can also translate it into the following if / else statements:

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

All Articles