Operator Overloading and C # Inheritance

Say I have a parent class and a child class as shown below

Parent class:

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


}

Child class:

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

}

And here is the code to compare two instances of the child.

 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();

Output

True true false false

I know why this gives truth and truth during the first comparison. Because it calls the overloaded operator == () of the parent class. My question is: I wanted to use the == operator of the child class, because the object is of type Child, how is this possible? For static methods, you cannot use the virtual keyword.

Thank,

+3
source share
2 answers

. : == ==.

== - Child, .

 Child p = new Child() { _First = "Mitul", _Last = "Patel", Address="abc1"};
 Child p1 = new Child() { _First = "Mitul", _Last = "Patel", Address = "abc" };

== Equals Child Equals :

Parent.cs:

// No need for these to be public- they should only be called internally.
protected virtual bool Equals(Parent that)
{
    if (ReferenceEquals(that, null))
        return false;
    else if (ReferenceEquals(that, this))
        return true;
    else
        return this._First.Equals(that._First) & this._Last.Equals(that._Last);
}

Child.cs:

// Notice I changed your argument type here...
protected override bool Equals(Parent that)
{
    // If we are not dealing with a Child instance, delegate to the base class.
    if (!(that is typeof(Child)))
        return base.Equals(that);

    if (ReferenceEquals(that, null))
        return false;
    else if (ReferenceEquals(that, this))
        return true;
    else
        return this.Address.Equals(that.Address) & base.Equals(that);
}
+8

static , .
p p1 Parent, == Parent.
, , .

+1

All Articles