Prashanth Benny Prashanth Benny - 1 year ago 86
ASP.NET (C#) Question

A confusion in the working of if condition statement in C#

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.

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

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download