Why does == not work while comparing two object type variables boxed with same int value

前端 未结 3 471
难免孤独
难免孤独 2021-01-18 06:57

While trying to implement a simple singly linked list in C#, I noticed that == does not work while comparing two object type variables boxed with an int value b

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 07:19

    The operator == is like an overloaded static function selected based on the compile time types. In your case the type of the values is Object, for which the == operator implements reference equality.

    On the other hand .Equals is virtual and overridden, so it will do the comparison based on the actual types.

提交回复
热议问题