加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 匿名类型的平等

发布时间:2020-12-15 04:24:31 所属栏目:百科 来源:网络整理
导读:参见英文答案 Why does the Equals implementation for anonymous types compare fields?4个 当用于比较匿名类型时,为什么Equals()和==的语义不同?为什么要比较值和其他比较参考?它背后的原因是什么? 解决方法 ==不调用Equals,它查找==重载运算符.由于匿
参见英文答案 > Why does the Equals implementation for anonymous types compare fields?4个
当用于比较匿名类型时,为什么Equals()和==的语义不同?为什么要比较值和其他比较参考?它背后的原因是什么?

解决方法

==不调用Equals,它查找==重载运算符.由于匿名类型没有重载==运算符,因此C#使用引用比较.

但是使用Equals可以比较字段值.这就是==和Equals之间的结果不同的原因.

Anonymous Types (C# Programming Guide)

Because the Equals and GetHashCode methods on anonymous types are
defined in terms of the Equals and GetHashCode methods of the
properties,two instances of the same anonymous type are equal only if all their properties are equal.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读