I compared the sources of both
Microsoft's and
Mono's implementation of
System.Drawing.Color
.
Equals
is overridden and each member is checked for equality.
Microsoft does this both in
Equals
and in
==
.
Mono does this in
==
, and calls
==
to test equality in
Equals
.
GetHashCode
is overriden. Microsoft XORed the member's hashcodes i.e.
value.GetHashCode() ^ state.GetHashCode()
etc.; Mono does the same but bit shifts each hashcode by 16 bits to the last.
Both define
!=
as
!(a == b)
.
Neither implements
IEquatable<color></color>
.
For anyone interested,
Microsoft's equality implementation:
public static bool operator ==(Color left, Color right) {
if (left.value == right.value
&& left.state == right.state
&& left.knownColor == right.knownColor) {
if (left.name == right.name) {
return true;
}
if (left.name == (object) null || right.name == (object) null) {
return false;
}
return left.name.Equals(right.name);
}
return false;
}
public static bool operator !=(Color left, Color right) {
return !(left == right);
}
public override bool Equals(object obj) {
if (obj is Color) {
Color right = (Color)obj;
if (value == right.value
&& state == right.state
&& knownColor == right.knownColor) {
if (name == right.name) {
return true;
}
if (name == (object) null || right.name == (object) null) {
return false;
}
return name.Equals(name);
}
}
return false;
}
public override int GetHashCode() {
return value.GetHashCode() ^
state.GetHashCode() ^
knownColor.GetHashCode();
}
}
Mono's equality implementation:
public static bool operator == (Color left, Color right)
{
if (left.Value != right.Value)
return false;
if (left.IsNamedColor != right.IsNamedColor)
return false;
if (left.IsSystemColor != right.IsSystemColor)
return false;
if (left.IsEmpty != right.IsEmpty)
return false;
if (left.IsNamedColor) {
if (left.Name != right.Name)
return false;
}
return true;
}
public static bool operator != (Color left, Color right)
{
return ! (left == right);
}
public override bool Equals (object obj)
{
if (!(obj is Color))
return false;
Color c = (Color) obj;
return this == c;
}
public override int GetHashCode ()
{
int hc = (int)(Value ^ (Value >> 32) ^ state ^ (knownColor >> 16));
if (IsNamedColor)
hc ^= Name.GetHashCode ();
return hc;
}