Unexpected behavior in c# generic method on .Equals

前端 未结 3 1729
孤独总比滥情好
孤独总比滥情好 2021-02-13 09:04

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

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-13 10:02

    TimezoneInfo defines it's own overload of Equals(TimeZoneInfo). In the Compare method, the object equals is used (it's a virtual method call of Object.Equals), whereas in Console.WriteLine(tzOne.Equals(tzTwo)) the overloaded (new) TimeZoneInfo.Equals method is called.

    TimeZoneInfo obviously hasn't overridden the Object.Equals method correctly...

提交回复
热议问题