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

  • Home
  • SEARCH
  • 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 150051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:13:44+00:00 2026-05-11T09:13:44+00:00

Okay, so if I override Equals, I need to override GetHashCode and vice versa.

  • 0

Okay, so if I override Equals, I need to override GetHashCode and vice versa.

But I just wonder: Should I always override these two in any class? Especially if I know that I will use them in a Dictionary or similar collections? While this is fairly straight forward, it’s still an extra work for every class.

Is the System.Object implementation bad enough to worry about this?

Edit: Can you detail a bit more what is Value and Reference equality? So if I have two strings (s1 and s2) that are both ‘test’, they are value equal, but since they are two different strings, they are not reference-equal? Ok, for strings it’s a no-brainer, but what are the common situations where you would want Reference or Value Equality?

  • 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. 2026-05-11T09:13:44+00:00Added an answer on May 11, 2026 at 9:13 am

    As far as I’m aware you only need to override them if you need value equality semantics. The System.Object implementation isn’t ‘bad’, it just only does a reference check (which is all an implementation at that level can do).

    In short: If you need some sort of value based equality (equality based on properties of the class), then yes, override away. Otherwise, it should be more than fine already.

    EDIT: You only need to override them in the case above. If you override one, you do need to override both for the obvious reasons (they need to be consistent, etc). You can override them on every class for other reasons noted in other answers (like performance in algorithms that use the hash value, ie. Dictionary keys, etc), but you don’t need to, the default System.Object implementation will work correctly.

    EDIT 2: More information requested, so here goes. Consider the following pseudo-class:

    public class User {     private int _id;     private string _username;     public string Username { get {return _username;}};     // [snip] Whatever other properties we might like to have.      public User(string username) {         // Initialise our user from a database, or whatever.     } } 

    As it stands, the following code, might seem intuitive:

    User foo = new User('me'); User bar = new User('me'); User baz = foo;  if (foo.Equals(bar)) {     Console.WriteLine('1: Success!'); } if (foo.Equals(baz)) {     Console.WriteLine('2: Success!'); } 

    But it will only print out:

    2: Success

    Why? foo and bar are two seperate instances of the class, and have separate references. A reference is like a pointer in C/C++. foo and baz are the same reference because one was assigned from the other. They all have the same value though, the user called ‘me’. A sample implementation of a value based .Equals implementation might be:

    partial class User {     public override bool Equals(object b) {         if (b == null) return false;         if (b.GetType() != this.GetType()) return false;          // Now the heavy lifting         User other = (User)b;         if (other._id == this._id) return true;         else return false;     } } 

    See how it checks against part of the class’s properties to determine equality? That’s value equality at work. Reference equality would just be a simple this == b check.

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

Sidebar

Related Questions

Okay, I think I'm just making a stupid mistake here, but I want to
Okay, these two related questions are in reference to Railscast #21 : I'm having
okay so this is probably a soft pitch question for sombody, but I want
Okay, I have done a bit of searching online and found this thread, but
Okay, before you guys go nuts -- this is just a small site, temporary
Okay, so I'm trying to override a function in a parent class, and getting
Okay, so recently I've been reading into ninject but I am having trouble understanding
Okay, i am new for android developping, or any developping and getting this error.
Okay, I know you can override the to_xml method for a single instance of
OKay gang, forgive me if this question is ubernoobness, but i am having trouble

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.