Sometimes, true != true. Let me explain. I'm from C++ where false is 0, and true is everything but 0. Since C# has a strongly typed Boolean type, I thought that the == and != operators were correctly implemented: that true == true whatever the byte value behind true was (except 0). This is not the case, and is not clearly documented. Boolean.Equals is not overridden, therefore the comparison is left to Object.Equals that compares the byte value. Even the CompareTo method won't return 0. In my opinion that is incorrect. Booleans should be bits, 0 or 1, and comparable as such. I shouldn't have to write "!bool1 == !bool2" or an helper method like in C++ to compare booleans, not in 2008.My problem started when writing unit tests with PEX. Sometimes, PEX will use boolean values other than true (0x01) and false (0x00), like true (0x02 or 0x80). While those values are indeed true, I encountered a lot of problems when creating assertions for comparing expected booleans.It uses the following method because that cast is safe in MSIL, but not allowed by the C# compiler:public static unsafe bool ByteToBoolean(byte b){ return *(((bool*) &b));}Those assertions will work if boolParameter is true/false, but will fail if it is (bool)0x02:PexAssert.AreEqual(boolParameter, boolResult);PexAssert.IsTrue(boolParameter == boolResult);
Version