账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    自学者请教 关于 c#对象值相等为什么要重写 Equals(object obj)的问题,自己查了几个小时资料也没解决
    32
    0

    自己尝试写出如下代码,来判断对象相等,虽然能正常工作。

    using System;
    
    namespace 比较相等
    {
        class Program
        {
            static void Main(string[] args)
            {
                
    
                Pet a1 = new Pet {  Name = "Turbo", Age = 8 };
                Pet a2 = new Pet { Name = "Turbo", Age = 8 };
    
              
           
                if (a1.Equals(a2))
                    Console.WriteLine("相等");
                else
                    Console.WriteLine("不相等");
               
    
                Console.Read();
    
            }
        }
        class Pet
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public  bool Equals(Pet other)
            {
                if ((Object) other ==null)
                {
                    return false;
                }
                return this.Name == other.Name && this.Age == other.Age ;
            }
               
    
        }
    
    }

    但是查网上的资料,发现微软官方和其他博客不仅写了 Equals(Pet other),还要重写 override Equals(object obj)。

    疑惑 1:

    官方说实现特定的 Equals(Pet other)是为了提高性能,这个我勉强能理解。但是为什么还要重写 Equals(object obj)呢? 它的意义何在? 或者说,什么样的情况下,明明已经有了 Equals(Pet other)不会调用 Equals(Pet other),而是去调用 Equals(object obj)?

    我尝试模仿官方文档,在 Pet 类中添加了 Equals(object obj),为了强行去调用 Equals(object obj)(疑惑 1 ),还单独创建了一个类 Gdd。

     using System;
    
    namespace 比较相等
    {
        class Program
        {
            static void Main(string[] args)
            {
                
    
                Pet a1 = new Pet {  Name = "Turbo", Age = 8 };
                Gdd a2 = new Gdd { Name = "Turbo", Age = 8 };
    
              
           
                if (a1.Equals(a2))
                    Console.WriteLine("相等");
                else
                    Console.WriteLine("不相等");
               
    
                Console.Read();
    
            }
        }
        class Pet
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public  bool Equals(Pet other)
            {
                if ((Object) other ==null)
                {
                    return false;
                }
                return this.Name == other.Name && this.Age == other.Age ;
            }
    
     public override bool Equals(object obj)
            {
               
    
                if (obj == null )
                {
                    return false;
                }
    
    
                Pet p = obj as Pet;
                if ((Object)p == null)
                {
                    return false;
                }
    
                return this.Name == p.Name && this.Age == p.Age;
            }
               
    
        }
    class Gdd
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    
    }
    

    疑惑 2:

    从逻辑上来说,a1 和 a2 的值是相等的,此时调用的是 Equals(object obj),不过永远返回 false。

    如以上代码中完全独立的两个类,怎么写 Equals 方法去判断 a1 和 a2 值相等?

    2
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 画船听雨眠~ 普通会员 1楼

      在C#中,Object类已经提供了重写Equals(object obj)方法,以确保类的成员数据的唯一性。重写这个方法可以让你自定义一个对象是否相等的方法。

      这个方法接受一个参数obj,它是一个已经存在对象。它会返回一个布尔值,表示这个对象的值是否相等。

      以下是一个简单的示例:

      ```csharp public class Person { public string Name { get; set; } public int Age { get; set; }

      public bool Equals(Person other)
      {
          if (other == null)
          {
              return false;
          }
      
          if (this.Name != other.Name)
          {
              return false;
          }
      
          if (this.Age != other.Age)
          {
              return false;
          }
      
          return true;
      }
      

      }

      public class Program { public static void Main(string[] args) { Person p1 = new Person { Name = "Alice", Age = 30 }; Person p2 = new Person { Name = "Bob", Age = 30 };

          Console.WriteLine(p1.Equals(p2));  // 输出: true
      }
      

      } ```

      在这个例子中,我们定义了一个Person类,它有两个属性:Name和Age。我们还定义了一个Equals方法,它会比较Person对象的Name和Age属性是否相等。如果是,它返回true,否则返回false。

      在main方法中,我们创建了两个Person对象p1和p2,它们的Name和Age属性都相同。因此,我们调用Equals方法,它返回true,这意味着p1和p2是相等的。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部