I came up with a simple error that can make lose some hours around something has simple has an operator.
imagine that you have the following operator definition:
public static bool operator ==(Player c1, Player c2) {
if( c1 == null && c2 == null ) {
return true;
}
if ( c1 == null || c2 != null ) {
return false;
}
return c1.Id == c2.Id;
}
The above code should work in a normal situation. But not in a situation where we use the operator inside it’s own definition. The code will enter a recursive state and it will eventually generate a Stack Overflow Exception.
So, in order to avoid the usage of the operator == inside it’s definition, you can use the alternative:
the Equals Type method inherit from object.
The following code demonstrates the correct implementation of the operator ==:
public static bool operator ==(Player c1, Player c2) {
if( Equals(c1,null) && Equals(c2,null) ) {
return true;
}
if ( Equals(c1, null) || Equals(c2, null) ) {
return false;
}
return c1.Id == c2.Id;
}
Your blog had all the great info I was looking for. Very enlightening. Any special product you recommend?