Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7906979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:12:36+00:00 2026-06-03T11:12:36+00:00

Let’s say I have a parent class and child class as below Parent Class:

  • 0

Let’s say I have a parent class and child class as 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 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 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.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:12:41+00:00Added an answer on June 3, 2026 at 11:12 am

    The implementation of the operator is chosen at compile time. Operators are not virtual methods- the child class’s == operator does not override the parent class == operator.

    Therefore the only way to make the compiler select the child == operator is to have the variables themselves be of type Child, e.g.

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

    or to have the == operator call the Equals method, and make the Child implementation of Equals override the parent implementation:

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

    in 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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say we have the following: abstract class A; class B : public A;
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the string: hello world; some random text; foo; How could
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have a simple UserControl with no codebehind: <UserControl xmlns= .... x:Class=TrafficLight.LightControl>
Let's say I have the following type type Key = String type Score =
Let's say I have an string variable called *magic_string* which value is set to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.