Comparing boxed value types

后端 未结 5 1036
轮回少年
轮回少年 2020-11-28 13:06

Today I stumbled upon an interesting bug I wrote. I have a set of properties which can be set through a general setter. These properties can be value types or reference type

5条回答
  •  孤独总比滥情好
    2020-11-28 14:04

    Equals() is generally the preferred approach.

    The default implementation of .Equals() does a simple reference comparison for reference types, so in most cases that's what you'll be getting. Equals() might have been overridden to provide some other behavior, but if someone has overridden .Equals() in a class it's because they want to change the equality semantics for that type, and it's better to let that happen if you don't have a compelling reason not to. Bypassing it by using == can lead to confusion when your class sees two things as different when every other class agrees that they're the same.

提交回复
热议问题