我特别关注引用类型中的内容相等性.在任何一种情况下,我都不会压倒平等 – 所以为什么行为会有所不同.
参见2个简单的代码示例:
示例1:返回True
class Program { static void Main(string[] args) { object o1 = "ABC"; object o2 = "ABC"; Console.WriteLine("object1 and object2: {0}",o1.Equals(o2)); } }
示例2:两个语句都返回False
class Program { static void Main(string[] args) { Person person1 = new Person("John"); Person person2 = new Person("John"); Console.WriteLine("person1 and person2: {0}",person1.Equals(person2)); Console.WriteLine("person1 and person2: {0}",((object)person1).Equals((object)person2)); Console.ReadLine(); } } public class Person { private string personName; public Person(string name) { this.personName = name; } }
解决方法
这里有两种效果:
>字符串实习意味着即使您执行引用相等性检查,您仍然会看到True.你可以像这样修复:
object o1 = new StringBuilder("ABC").ToString(); object o2 = new StringBuilder("ABC").ToString();
> System.String overrides the Equals
method比较字符串的内容:
This method performs an ordinal (case-sensitive and culture-insensitive) comparison.
你可以在这里看到不同之处:
object o1 = new StringBuilder("ABC").ToString(); object o2 = new StringBuilder("ABC").ToString(); Console.WriteLine(o1.Equals(o2)); // Prints True due to overriding Console.WriteLine(ReferenceEquals(o1,o2)); // Prints False
你的类不会覆盖Equals,所以你得到default implementation in Object
,这是比较引用:
If the current instance is a reference type,the Equals(Object) method tests for reference equality,and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method.
你可以通过重写Equals来合理地解决这个问题:
// It's easier to implement equality correctly on sealed classes public sealed class Person { private readonly string personName; public Person(string name) { if (name == null) { throw new ArgumentNullException("name"); } this.personName = name; } public override bool Equals(object other) { Person person = other as Person; return person != null && person.personName.Equals(personName); } // Must override GetHashCode at the same time... public override int GetHashCode() { // Just delegate to the name here - it's the only thing we're // using in the equality check return personName.GetHashCode(); } }
请注意,在Equals实现中,我们可以使用:
return person != null && person.personName == personName;
…因为string也会重载==运算符.但那是另一回事:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。