码迷,mamicode.com
首页 > Windows程序 > 详细

C#重载==和!=

时间:2016-12-01 11:43:13      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:err   ann   ret   重载   int   equal   settings   operator   hash   

class A

{

  public int P1 { get; set; }

  public int P2 { get; set; }

  public static bool operator !=(A a1, A a2)
  {
    return !a1.Equals(a2);
  }

  public static bool operator ==(A a1, A a2)
  {
    return a1.Equals(a2);
  }  

  

  public override bool Equals(object obj)
  {
    // If parameter is null return false.
    if(obj == null)
    {
      return false;
    }  

    // If parameter cannot be cast to Settings return false.
            A a2 = obj as A;
    if((System.Object)a2 == null)
    {
      return false;
    }

    // Return true if the fields match:
    if (P1 != a2.P1 || P2 != a2.P2)
      return false;
    else
      return true;
     }

  public override int GetHashCode()
  {
    return P1;
  }

}

 

C#重载==和!=

标签:err   ann   ret   重载   int   equal   settings   operator   hash   

原文地址:http://www.cnblogs.com/shaoxuejun/p/6121160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!