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 8005165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:09:02+00:00 2026-06-04T17:09:02+00:00

For the code below public struct Person { public int ID; public static bool

  • 0

For the code below

public struct Person
{
    public int ID;
    public static bool operator ==(Person a, Person b) { return  a.Equals(b); }
    public static bool operator !=(Person a, Person b) { return !a.Equals(b); }
}

Why does the compiler give me these warnings?
What’s wrong with not defining the methods below?

warning CS0660: 'Person' defines operator == or operator != but
    does not override Object.Equals(object o)

warning CS0661: 'Person' defines operator == or operator != but
    does not override Object.GetHashCode()
  • 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-04T17:09:04+00:00Added an answer on June 4, 2026 at 5:09 pm

    EDIT: This answer has been corrected, among other things to note that user-defined value types don’t generate ==, and to mention the performance issues with ValueType.Equals.


    In general, overridding one, but not all, is confusing. The user expects neither to be overridden, or both to be, with the same semantics.

    Microsoft’s recommendations for this state (among other things):

    • Implement the GetHashCode method whenever you implement the Equals method. This keeps Equals and GetHashCode synchronized.

    • Override the Equals method whenever you implement the equality operator (==), and make them do the same thing.

    In your case, you have a valid reason to defer to Equals (the compiler doesn’t automatically implement ==) and override only those two (==/!=). However, there’s still a performance issue, since ValueType.Equals uses reflection:

    “Override the Equals method for a particular type to improve the
    performance of the method and more closely represent the concept of
    equality for the type.”

    Thus, it’s still recommended to override all (==/!=/Equals) in the end. Of course, performance may not matter for this trivial struct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below : public class Anything { public int Data {
Please find the relevant snippets of my code below: public static final String GREEK
I try use a integer array in java with the code below: public static
my code like below public class User { public int ID { get; set;
struct mydata { public int id; public string data; } class Program { static
The code below does not compile in Visual C++ 2005. class SomeClass { public:
Why is it that in the code below template<typename T> struct Child : public
While trying to compile the code below: template <class T> struct scalar_log_minimum { public:
I am working with NHibernate and I get this code below: public class User
Code below: [HttpGet] public ActionResult Edit(string id=) { // ... } [HttpPost] public ActionResult

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.