c# – 匿名类型的平等

参见英文答案 > 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.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么