C#重载==和!=


class A

{

  public int P1 { get; set; }

  public int P2 { get; set; }

  public static bool operator !=(A a1, A a2)
  {

         if((a1 as object) != null)
      return !a1.Equals(a2);

    else

      return (a2 as object) != null;
  }

  public static bool operator ==(A a1, A a2)
  {
         if((a1 as object) != null)

      return a1.Equals(a2);

    else

      return (a2 as object) == null;
  }  

  

  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;
  }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM