c# – 参考类型中的平等

前端之家收集整理的这篇文章主要介绍了c# – 参考类型中的平等前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我特别关注引用类型中的内容相等性.在任何一种情况下,我都不会压倒平等 – 所以为什么行为会有所不同.

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

原文链接:https://www.f2er.com/csharp/100720.html

猜你在找的C#相关文章