Exact duplicate:
Why does one often see “null != variable” instead of “variable != null” in C#?
I have seen senior developers using syntaxes mentioned in the title.
Is there a need for specifying a constant first in .NET? (as opposed to in C/C++ world)
No, there’s no need for this, because the problem it tries to avoid – namely the typo of:
wouldn’t compile in C# anyway. The conditions in
ifstatements have to be Boolean. There’s still a risk of making one of these mistakes:if
somethingis a Boolean variable, but the better way to fix this is to avoid the constant:If you have developers bringing over idioms like this from other languages without thinking about whether they’re appropriate in C#, you should keep an eye for them doing more of the same. If you try to write C# as if it’s C++ (or any other language, pretty much – with the possible exception of VB.NET) you’ll end up writing non-idiomatic C# code.
EDIT: As cletus noted, there is another potential area for concern:
So the error can still occur – but then we’re outside the realm of comparing with a constant anyway 🙂 I’d say this crops up incredibly rarely, and isn’t worth too much time spent worrying about it.