Why does the Equals method return a different result from within the generic method? I think that there\'s some automatic boxing here that I don\'t understand.
Here\'s a
TimeZoneInfo does not override the Object Equals method, so it calls the default Object Equals, which apparently does not work as expected. I would consider this a bug in TimeZoneInfo. This should work:
private static Boolean Compare(T x, T y)
where T: IEquatable
{
if (x != null)
{
return x.Equals(y);
}
return false;
}
The above will cause it to call Equals
, which is the method you were calling above (it implicitly preferred the generic call because it was more specific to the parameter type than the Object Equals; inside the generic method, however, it had no way to be sure that such a generic Equals existed, since there was no constraint guaranteeing this).