newbie_007

Hi,

Can anyone explain the below given pattern of using if-else

Code Snippet

bool disposing = false;

if(disposing)

{

do something;

}

if(!disposing)

{

do something;

}

How is above given snippet different from the below one

Code Snippet

bool disposing = false;

if(disposing == false)

{

do something;

}

if(disposing == true)

{

do something;

}

This may seem silly, but I'm not understanding this.



Re: Visual C# Language Very basic question

George Olson

They are the same thing. When disposing is false, !disposing and disposing == false are both true and code in the block executes. When disposing is true, disposing and disposing == true are both true and the code block executes. In other words when the condition within the if statement is true, the code will execute. Lets say that disposing is false.

If disposing is false

if (disposing) = if(false)

if(!disposing) = if (not false) = if(true)

if(disposing == false) = if(true) because it is true that disposing does equal false

if(disposing == true) = if(false) because it is false that disposing does not equal true





Re: Visual C# Language Very basic question

Peter Ritchie

newbie_007 wrote:

Hi,

Can anyone explain the below given pattern of using if-else

Code Snippet

bool disposing = false;

if(disposing)

{

do something;

}

if(!disposing)

{

do something;

}

How is above given snippet different from the below one

Code Snippet

bool disposing = false;

if(disposing == false)

{

do something;

}

if(disposing == true)

{

do something;

}

This may seem silly, but I'm not understanding this.

Code Snippet

if(disposing)

// do something

if(!disposing)

// do something else

is actually equivalent to:

Code Snippet

if(disposing == true)

// do something

if(disposing == false)

// do something else






Re: Visual C# Language Very basic question

OmegaMan

In your mind just think of the ! as a reverse / not of whatever is the output. So process the information and then lastly flip it for the final answer.





Re: Visual C# Language Very basic question

Marcelo Guerra - MSFT

Hi,

As they are equivalent I suggest using the small version as it has one less calculation.

Marcelo.





Re: Visual C# Language Very basic question

Mohamed Shifaz

I agree with Marcelo.