I'm doing some experiments with structs and operators, and I came across a situation that I cannot understand.
I have a struct that only contains an int
. I've also implemteted 'implicit operator' methods so I can assign an int
directly to it and I've implemented the Equality operators
Everything seems to be working fine at runtime, but if I make a breakpoint, and execute this ((TestStruct)1) == ((TestStruct)1);
in the Immediate Window it returns false, but at runtime it returns true (as I was expecting).
If I put another breakpoint, on the Equality operator, I can see that the code in there is being executed, but the values for my structs are not '1' as I was expecting, but some random ones.
Here is my sample code:
class Program
{
static void Main(string[] args)
{
bool areEqual = ((TestStruct)1) == ((TestStruct)1);
string breakPoint = ";)";
}
}
struct TestStruct
{
private Int32 value;
public TestStruct(Int32 value)
{
this.value = value;
}
static public implicit operator TestStruct(Int32 value)
{
return new TestStruct(value);
}
public static bool operator ==(TestStruct ptr1, TestStruct ptr2)
{
return ptr1.value == ptr2.value;
}
public static bool operator !=(TestStruct ptr1, TestStruct ptr2)
{
return ptr1.value != ptr2.value;
}
}
Edit It seems to work fine if used with VS 2013, this issue seems to occur only with VS 2015
It seems to be a Visual Studio 2015 bug as Hans Passant found out
Thanks ;)