I’ve just come across this code snippet in some Exchange 2010 code and I was wondering if anyone knew why the programmer has done it this way. I’ve never seen an If statement formatted like this. It seems so backwards there must be a good reason for it??
if (true == MsgItem.HasAttachments)
{
// Code
}
I’m assuming it might have some optimisation over the various other ways of coding the same thing;
if (MsgItem.HasAttachments)
{
// Code
}
or
if (MsgItem.HasAttachments == true)
{
// Code
}
Its not a big deal I’m just curious.
Thanks,
Mike
UPDATE: Thanks for all the interesting points raised. Summary seems to be it’s down to legacy coding standards.
It is a left-over habit from C or C++ where you might accidentally do:
This accidentally assigns
1toiinstead of doing a comparison.Reversing the order in the check prevents this common mistake.
That will not compile, and it is easy to see that you mistakenly typed
=instead of==.This is still relevant for
boolsin C# because the value of an assignment is the value that was assigned.Therefor:
Will always be true because you are storing
trueintoSomeBoolVarwhich evaluates totrue. A little mistake that can be hard to track down.