Why is null == myVar instead of myVar == null?

Possible duplicate:
Why is C # often displaying "null! = Variable" instead of "variable! = Null"?

I see this from time to time, and I would like to know why. Is there any difference at all?

+6
source share
6 answers

This is an old habit of avoiding accidental typos myVar = null (screaming). It is still useful in some languages, but C # will protect you from running it, so itโ€™s not necessary there.

+20
source share

This is a save from C, where older compilers will not understand this:

 if (foo = null) 

when you mean this:

 if (foo == null) 

A classic joke example is a mistake:

 if (fireTheNukes = true) fireTheNukes(); 

This is generally accepted as an archaic template, since any compiler deserving its salt will catch the assignment in a conditional expression. I would avoid this pattern in your code, because these days it has no purpose.

+8
source share

This is an old defensive habit. If you put a constant on the left, then forgetting the second equal sign will lead to a compilation error. In a more normal format, forgetting the second equal sign leads to the assignment of a null variable.

In other words,

 myVar = null 

is harmful and surprising whereas

 null = myVar 

gets into the compiler.

+2
source share

This is the habit of C programmers, which was ported to C # in this case, but actually not needed at all.

Consider in C that if you accidentally type if (myVar = null) , the compiler will complete the task and will not complain. Switching the order of myVar and null ensures that a compiler error will be generated if == mistakenly saddened as = . However, C # generates a compiler warning anyway, so this quirk is not needed.

+2
source share

It comes from C / C ++ to catch one equal sign:

 myVar = null 

But it is not needed in C #.

+2
source share

As others have noted, this is a defensive tactic against problems that might arise because in C and some C derivatives (including C #), an assignment expression evaluates the value of this assignment. This allows you to:

 if (a = true) { /* This will always get done, as "a = true" evals to true */ } 

and

 int a = b = c = d = 10; 

Since assignment is the correct associative, it is effective

 int a = (b = (c = (d = 10))); 

where each expression inside a pair of curly braces will evaluate the assignment value, which in this case is 10 and a, b, c and d will thus be 10.

To avoid possible mistakes - mixing assignment and equality operators - some programmers prefer to always put a constant on the left, as if the assignment operator was accidentally used, the compiler will complain that you cannot assign a constant.

This, however, has fewer problems in C # for two reasons. Firstly, unlike C, C # does not allow interpreting arbitrary values โ€‹โ€‹as a logical value.

This was necessary in C because it did not have a true boolean type, it simply relied on interpreting other values, such as integers (where 0 is false and not zero is true) or pointers (where NULL is false). That meant you could do something like

 if (10) { /* This will always get done */ } if (0) { /* This will never get done */ } if (p) { /* This will get done is p is not null */ } if (NULL) { /* This will never get done */ } 

However, since C # does not allow arbitrary expressions to be interpreted as logical, they will not work in C #. It also means that

 if (a = 10) { } 

will not compile in C #, since the expression "a = 10" evaluates to the value of expression 10, which then cannot be interpreted as the required logical value.

The second reason this is less is that in a much lower percentage of cases where the result of an assignment can be interpreted as a boolean, the compiler issues a warning to make sure you really mean it.

Warning can be suppressed with

 #pragma warning disable 665 

However, the presence of such code is often a bad smell of code, and refactoring is probably best to make the code more understandable.

+1
source share