Prashanth Benny Prashanth Benny - 1 month ago 4x
ASP.NET (C#) Question

If condition with a ternary operator and a Greater than(>) symbol

I am confused with the following piece of code

bool i_am = true;
int he_is= 0;
if ((!i_am? false : he_is> 0))
Console.WriteLine("Condition is true.....");

i have tested tried all possible ways to understand by testing it with different possibilities of i_am and he_is but still am not able to find the logic.

Tried removing one set of the braces "()" and it produces a different output.
the braces also work confusingly!

I am a beginner in C#,could anyone please help me with the flow of code in this condition statement.
Thank you in Advance.


If we analyze it piece by piece:

!i_am ? false : he_is > 0

is equal to typing:

if (!i_am) //The ! operator means "NOT", so its like saying if(i_am = false), while the default if(i_am) would be equal to if(i_am = true)
     return false;
     return he_is > 0; //he_is = 0, so 0 > 0 will return false.