1 Answer

0 votes
by

Yes, it is important if your item will be used as a key in a dictionary, or HashSet<T>, etc - since this is used (in the absence of a custom IEqualityComparer<T>) to group items into buckets. If the hash-code for two items does not match, they may never be considered equal 

The GetHashCode() method should reflect the Equals logic; the rules are:

  • if two things are equal (Equals(...) == true) then they must return the same value for GetHashCode()
  • if the GetHashCode() is equal, it is not necessary for them to be the same; this is a collision, and Equals will be called to see if it is a real equality or not.

In this case, it looks like "return FooId;" is a suitable GetHashCode() implementation. If you are testing multiple properties, it is common to combine them using code like below, to reduce diagonal collisions (i.e. so that new Foo(3,5) has a different hash-code to new Foo(5,3)):

unchecked // only needed if you're compiling with arithmetic checks enabled
{ // (the default compiler behaviour is *disabled*, so most folks won't need this)
    int hash = 13;
    hash = (hash * 7) + field1.GetHashCode();
    hash = (hash * 7) + field2.GetHashCode();
    ...
    return hash;
}

Oh - for convenience, you might also consider providing == and != operators when overriding Equals and GetHashCode.

Related questions

0 votes
    Why we override equals() method?...
asked May 27, 2021 in Technology by JackTerrance
0 votes
    Why is it that in some cases, SQL override is used?...
asked Mar 27, 2021 in Technology by JackTerrance
0 votes
    _jspService() method of HttpJspPage class should not be overridden. (a) True (b) False This question was posed to me ... & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    _jspService() method of HttpJspPage class should not be overridden. (a) True (b) False This question was ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    In the following circuit, when R = 0 Ω, the current IR equals to 10 A. The value of R, for which ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 20, 2021 in Education by JackTerrance
0 votes
    I can't explain this error in the game I am trying to make. I'm pretty new to game programming, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I can't explain this error in the game I am trying to make. I'm pretty new to game programming, ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    When do you use SQL override in a lookup transformation?...
asked Mar 26, 2021 in Technology by JackTerrance
0 votes
    What is performance testing and why do you think it is important?...
asked Feb 8, 2021 in Technology by JackTerrance
0 votes
    What is PEP 8 and why is it important?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    I've noticed on Marshmallow (e.g. Nexus 6P) and also on some more recently updated Lollipop phones ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    The data type mapping between the database column and sqoop column can be overridden by using the parameter A - --override- ... - --override-column-java D - --map-column-java...
asked Jan 13, 2021 in Technology by JackTerrance
0 votes
    What is the difference between equals() and method and == operator? Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    Why is it important to conduct elections? Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
0 votes
    What is the dependency inversion principle and why is it important? JavaScript questions and answers, JavaScript ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 31, 2022 in Education by JackTerrance
...