Saurabh Surana

I have heard that in C# .Net the notation
if (flag)

should be prefered over

if(flag == true)

or

if(true == flag)


Is there a difference in above statements

Which one gives better performance and why



Many Thanks,




Re: Visual C# Language Which One is faster????

Matthew Watson

There is no difference in the generated IL code. The compiler will optimize all of the above to the same code.

However, most people name boolean variables in the form of a question:

"isComplete", "isError", "isSorted", "areEnabled" etc etc.

In that case, saying:

if (isComplete)

is more readable (and shorter) than:

if (isComplete == true)

or the (hideous and completely unneccessary):

if (true == isComplete) // shudder





Re: Visual C# Language Which One is faster????

Rob Hanz

The form:

int a = 0;
if( 1 == a )
....

is a c-ism, and a relatively useful one. C will allow you to do the following:
int a = 0;
if( a = 1 ) // note, we're not using == here...
.....

This will happily set a to 1, and evaluate to 1, which in C terms is true. This will make the conditional code run, which is probably *not* what the programmer intended. OTOH, if you use the 'value == variable' form, you'd get:

if( 1 = a )
.....

which would attempt to assign the value of a to the constant 1, which will cause a compilation error, which can help prevent a lot of debugging.

This isn't necessary in C#, as C# won't allow the first form to work. C# requires a boolean value in a conditional, so when a = 1 evaluates to '1', it generates a compiler error. You can still do assignment as a conditional with something along the lines of:

if( ( a = 1 ) == 1 )
....

The given example here is pretty silly, but if assigning a to the return value of a function, it's a pretty useful construct. For instance, if( ( a = foo() ) == someval ) may be useful in some situations.

However, C# *will* allow the following to work:

bool b = false;
if( b = true )
.....

which is, again, probably not what the programmer wanted. This works because the general form of 'var = val' will evaluate to the type of var, which in this case is bool, and so C# says "Hey, I've got a bool in my conditional, I'm good to go!" Fortunately, the compiler *is* smart enough to realize that this is probably not what you wanted, and at least generate a warning for you.

Using the form:

if( b )
....

prevents this error, and is probably preferred for that reason.




Re: Visual C# Language Which One is faster????

Saurabh Surana

There is one more doubt i had in my mind.
in case of strings, if i have to check if string is equal to "" then
which is the better way of doing it....
1) if( str == "")
or
2) if(str.Length == 0)
or
3) if(str.Equals(""))

Which of the above statement is better and why







Re: Visual C# Language Which One is faster????

Matthew Watson

Definitely quicker to do "if (str.Length == 0)"




Re: Visual C# Language Which One is faster????

Saurabh Surana

Can you explain me the reasion
Why is that one quicker

How the performance is benefited by using if(str.Lenght == 0)...





Re: Visual C# Language Which One is faster????

boban.s

Install FxCop, which comes with Visual Studio but is not installed. You can also download from here:
http://www.gotdotnet.com/team/fxcop/

Warning about "stringvalue == string.Empty" or "stringvalue == """ is here
http://msdn2.microsoft.com/en-us/library/da1e70c8-b1dc-46b9-8b8f-4e6e48339681(vs.80).aspx

This is great tool to point on bad programming and also show how to solve that problem and reason why is that bad programming practice.




Re: Visual C# Language Which One is faster????

Flip

There is also the String.IsNullOrEmpty(yourStringVariableName) to check for null and empty string. Since this is a String method, I assume (maybe rightly, maybe wrongly) that MS has implemented it the best way possible.





Re: Visual C# Language Which One is faster????

Matthew Watson

Yes, if you inspect the code for it (using Reflector), it looks like this:



public static bool IsNullOrEmpty(string value)
{
if (value != null)
{
return (value.Length == 0);
}
return true;
}



Of course, if you know the string is not null, it's quicker to just compare value.Length directly yourself.