I'm trying to find where in ECMA-334 (C# language specification) the following behavior is defined. The source program is as follows.
static void Main(string[] args)
{
TestStruct a = new TestStruct();
a.byteValue = 1;
TestStruct b = new TestStruct();
b.byteValue = 2;
Console.WriteLine(string.Format("Result of {0}=={1} is {2}.",
a.boolValue, b.boolValue, a.boolValue == b.boolValue));
Console.WriteLine(string.Format("Result of {0}!={1} is {2}.",
a.boolValue, b.boolValue, a.boolValue != b.boolValue));
Console.WriteLine(string.Format("Result of {0}^{1} is {2}.",
a.boolValue, b.boolValue, a.boolValue ^ b.boolValue));
}
[StructLayout(LayoutKind.Explicit, Pack = 1)]
struct TestStruct
{
[FieldOffset(0)]
public bool boolValue;
[FieldOffset(0)]
public byte byteValue;
}
The result of execution is the following.
Result of True==True is False.
Result of True!=True is True.
Result of True^True is True.
This violates both sections §14.9.4 and §14.10.3, so I'm assuming there's an exception stated elsewhere which covers these cases. Note that this does not affect code using AND, OR, NAND, or NOR operations, but it can affect code using XOR and/or logical biconditional operations.