July 15, 2004

if comparisons in properties

In C++, when I compare variables against constants and "value" in a property "set" method, I put “value” first like so:

if (value == _myProp)

This habit prevents the following catastrophic typo:

if (_myProp = value)

For most types, this statement won't compile in C#. The resulting statement is equal to the resulting type and the if clause expects only a bool. The above line would fail to compile If _myProp were an int. Seeing this for the first time, I was almost ready to switch back to the more attractive form:

if (_myProp == value)

Then I thought what if _myProp is a bool? As you can expect, the line compiles just fine. Therefore, I'm back to the "safe" form of the if statement:

if (value == _myProp)

 

Posted by Nick Codignotto at July 15, 2004 08:23 AM
Posted to Programming
Comments
Post a comment









Remember personal info?






Valid XHTML 1.0!   Valid CSS!