1

this is related to comparing values in C#.

basically by default, in C# till date i only used forward comparison as follows:

string value1 = "";

if (value1 == null) 
{ 
    Console.WriteLine("True"); 
} 
else 
{ 
    Console.WriteLine("False"); 
} 

somewhere on Internet, i came across a link where it is said that while comparing values in C#, we should prefer Reverse Comparison as :

string value1 = "";

if (null == value1)  
{ 
    Console.WriteLine("True"); 
} 
else 
{ 
    Console.WriteLine("False"); 
} 

Now the problem is that is there any basic difference between the two ways of comparing values or not (i.e. both are same).

Looking for favorable replies.

Thanks

Gunwant Saini
  • 317
  • 3
  • 10
  • 17
  • 3
    personally, (null == value1) read to hard very is. – Inisheer Dec 15 '10 at 03:08
  • 1
    @JTA: some people just follow the "best practices" even though they don't get why ;-) – zerkms Dec 15 '10 at 03:10
  • possible duplicate of [Why does one often see "null != variable" instead of "variable != null" in C#?](http://stackoverflow.com/questions/271561/why-does-one-often-see-null-variable-instead-of-variable-null-in-c) – Gabe Dec 15 '10 at 03:21
  • As, we use `int i=1; j=2;....` so its easy to follow same pattern `if(i==1)` – Javed Akram Dec 15 '10 at 03:25

2 Answers2

4

This is a hangover from languages where values are implicitly converted to a boolean type.

For the most part, anything that was non-null or non-0 would be treated as true, otherwise it was equivalent to false. This meant that any assignation of a constant would always be true.

This thing to remember here is that the assignment function returns the value assigned, much in the same way that 5 is the result of 2+3, myInt = 5 also returns 5.

Because of this, it was recommended that you check the variable against the constant. The null example is actually probably a bit contrived in this case, as it would, in most languages, return as false.

As an example:

if(i = 1)
{
    printf("Always out\n");
}
else
{
    printf("Never prints\n");
}

i is assigned the value of 1, which then returns the value of the assignment, 1, to the if statement. This is equivalent to true, and the if condition matches.

If you were to attempt to assign i to the constant 1, you wouldn't compile, and so it was recommended that you do things it that order (e.g. 1 == i).

This is not neccesary in C#, and further to your example, the recommended practice is to call if(String.IsNullOrEmpty(myStringValue)) { //.... }, as a string has... two ... default values from a semantic perspective.

Khanzor
  • 4,830
  • 3
  • 25
  • 41
  • C# lets you assign inside an if condition (`if((i = 1) == 1)` is useless, but compiles). However, only bools can be used directly *as* conditions, so in C# this mistake only occurs (rarely) for bools. – Matthew Flaschen Dec 15 '10 at 04:16
  • @Matthew Flaschen - you are quite correct. The behaviour occurs because the return value of assignment is the actual value itself. I'll update my answer to make that clearer :). – Khanzor Dec 15 '10 at 05:15
  • actually i have raised this question simply to know that whether this reverse comparison or forward comparison has any relation with performance or efficiency or not, that's it. it is also true that reverse comparisons are hard to read. – Gunwant Saini Dec 15 '10 at 17:20
  • @Everest - no, there is no perf benefit AFAIK, and I don't see how there could be. Even if it were more efficient to do comparisons that way, I'm sure the compiler would just switch the order around automatically. – Khanzor Dec 15 '10 at 21:18
1

Reverse comparison protects you from errors, when you use == (compare) instead of = (assign). But complier also warns you if you do this.

The Smallest
  • 5,713
  • 25
  • 38