Question about overriding ==

Posted on 2006-06-05
Last Modified: 2010-04-16
To overload the == operator, must I override the Equals and GetHashCode methods inherited from the Object class? Or are they static?
Question by:igor92128
    LVL 13

    Accepted Solution

    You dont have to.  You overload them if you want to provide a different implementation for them.  Perhaps you may want to override equals to provide the same implementation as your overloaded == operator.  You should probably do this for consistency but again it is not required.

    Author Comment

    This Question was unclear, let me re-phrase it:

    To overload the == operator, you must override

          a.       the Equals and GetHashCode methods inherited from the Object class.  
          b.       the static Equals and GetHashCode methods of the Object class.  
          c.       the Equals and GetHashCode properties inherited from the Object class.  
          d.       only the Equals method inherited from the Object class.  

    Author Comment

    it must be A...
    LVL 13

    Expert Comment

    That is a tricky question here is why,

    When you overload ==, C# insists that you also overload !=.

    After overloading == & != C# will WARN you that you havent overriden the Equals() method.

    After overloading ==, !=, & Equals() C# will then warn that you haven't overloaded GetHashCode().

    That question is unfair because it says "you must override."  Is the must inclusive of warnings?  From those rather poor choices I'd say d. is the best fit.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now