title: operator overloading and inheritance c#
body: <p>Let's say I have a parent class and child class as below</p>
  
  <p>Parent Class:</p>
  
  <pre><code>class Parent
  {
      public string _First;
      public string _Last;
  
      public override bool Equals(object obj)
      {
          if (ReferenceEquals(obj, null))
              return false;
          else if (ReferenceEquals(obj, this))
              return true;
          else if (obj is Parent == false)
              return false;
          else
              return this.Equals(obj as Parent) &amp; base.Equals(obj);
  
      }
  
      public override int GetHashCode()
      {
          unchecked
          {
              return this._First.GetHashCode() ^ this._Last.GetHashCode() ^ base.GetHashCode();
          }
      }
  
      public bool Equals(Parent that)
      {
          if (ReferenceEquals(that, null))
              return false;
          else if (ReferenceEquals(that, this))
              return true;
          else
              return this._First.Equals(that._First) &amp; this._Last.Equals(that._Last);
      }
  
      public static  bool operator ==(Parent p1, Parent p2)
      {
          return p1.Equals(p2);
      }
  
      public static  bool operator !=(Parent p1, Parent p2)
      {
          return !p1.Equals(p2);
      }
  
  
  }
  </code></pre>
  
  <p>Child Class:</p>
  
  <pre><code>  class Child : Parent
  {
      public string Address;
  
      public override bool Equals(object obj)
      {
          if (ReferenceEquals(obj, null))
              return false;
          else if (ReferenceEquals(obj, this))
              return true;
          else if (obj is Parent == false)
              return false;
          else
              return this.Equals(obj as Child);
  
      }
  
      public override int GetHashCode()
      {
          unchecked
          {
              return this._First.GetHashCode() ^ this._Last.GetHashCode() ^ base.GetHashCode();
          }
      }
  
      public bool Equals(Child that)
      {
          if (ReferenceEquals(that, null))
              return false;
          else if (ReferenceEquals(that, this))
              return true;
          else
              return this.Address.Equals(that.Address) &amp; base.Equals(that);
      }
  
      public static  bool operator ==(Child p1,Child p2)
      {
          return p1.Equals(p2);
      }
  
      public static bool operator !=(Child p1, Child p2)
      {
          return !p1.Equals(p2);
      }
  
  }
  </code></pre>
  
  <p>And here is the code to compare two instances of child.</p>
  
  <pre><code> Parent p = new Child() { _First = "Mitul", _Last = "Patel", Address="abc1"};
          Parent p1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };
  
          Child c = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc1" };
          Child c1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };
  
          Console.WriteLine(p.Equals(p1));
          Console.WriteLine(p == p1);
  
          Console.WriteLine(c.Equals(c1));
          Console.WriteLine(c == c1);
  
          Console.Read();
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <blockquote>
    <p>True True False False</p>
  </blockquote>
  
  <p>I know why it gives true and true during first comparison. Because it calls the overloaded ==() operator of parent class. My question is I wanted to use == operator of child class because the object is of type Child so how can it be possible? For static methods virtual keyword can not be used.</p>
  
  <p>Thanks,</p>