gpt4 book ai didi

.net - 覆盖 Equals 方法时是否需要覆盖 == 和 != 运算符? (。网)

转载 作者:行者123 更新时间:2023-12-04 02:33:06 27 4
gpt4 key购买 nike

或者建议这样做?
为什么?

最佳答案

guidelines for overriding Equals() and operator== .

引用:

By default, the operator == tests for reference equality by determining whether two references indicate the same object. Therefore, reference types do not have to implement operator == in order to gain this functionality. When a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator == to compare value equality instead of reference equality can be useful because, as immutable objects, they can be considered the same as long as they have the same value. It is not a good idea to override operator == in non-immutable types.



基本上:

如果你想让 == 和 != 表现得像 Equals(..)!Equals(..)你需要实现运营商。您通常只对不可变类型执行此操作。

关于.net - 覆盖 Equals 方法时是否需要覆盖 == 和 != 运算符? (。网),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1222035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com