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

c# – 参考类型中的平等

发布时间:2020-12-15 08:06:17 所属栏目:百科 来源:网络整理
导读:我特别关注引用类型中的内容相等性.在任何一种情况下,我都不会压倒平等 – 所以为什么行为会有所不同. 参见2个简单的代码示例: 示例1:返回True class Program{ static void Main(string[] args) { object o1 = "ABC"; object o2 = "ABC"; Console.WriteLin
我特别关注引用类型中的内容相等性.在任何一种情况下,我都不会压倒平等 – 所以为什么行为会有所不同.

参见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也会重载==运算符.但那是另一回事:)

(编辑:李大同)

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

    推荐文章
      热点阅读