6

I am using Entity Framework 5. In my C# code I want to compare if two objects are equal. If there are not then I want to issue an update.

I have been told I need to override the .Equals method and then also the gethascode method. My classes look like this:

public class Students    {
    public int PersonId { get; set; }
    public string Name { get; set; }
    public int Age {get; set;} 
}

Can some explain why I need to override .Equals and .GetHashCode. Also can someone give me an example. In particular I am not sure about the hashcode. Note that my PersonId is a unique number for this class.

  • 3
    http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden – Prabhu Murthy Aug 11 '13 at 04:42
  • @Melina you have asked multiple times in the comments why you need to override `GetHashCode`. Please *read the answers to the question that @CodeIgnoto has linked to*. – Hamish Smith Aug 11 '13 at 05:33

2 Answers2

3

You need to override the two methods for any number of reasons. The GetHashCode is used for insertion and lookup in Dictionary and HashTable, for example. The Equals method is used for any equality tests on the objects. For example:

public partial class myClass
{
  public override bool Equals(object obj)
  {
     return base.Equals(obj);
  }

  public override int GetHashCode()
  {
     return base.GetHashCode();
  }
}

For GetHashCode, I would have done:

  public int GetHashCode()
  {
     return PersonId.GetHashCode() ^ 
            Name.GetHashCode() ^ 
            Age.GetHashCode();
  }

If you override the GetHashCode method, you should also override Equals, and vice versa. If your overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode method must return the same value for the two objects.

HappyTown
  • 6,036
  • 8
  • 38
  • 51
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • I think he said that the ID is unique ^ – Mark Segal Aug 11 '13 at 04:48
  • Yes Id is unique. Can I just use this for the hashcode and why do I need to create a GetHashCode. How is it used? –  Aug 11 '13 at 04:51
  • @Melina if you are using a hashTable, dictionary, or some other things then the GetHashCode is called to get the hash of the class. you can do hashing only from the id, but that's your choice, i would have done hashing to all the members, but that's not mandatory if the ID is indeed unique – No Idea For Name Aug 11 '13 at 04:54
  • Thanks but I keep hearing "If I override .Equals" I need to override GetHashCode. So if I normally don't override .Equals do I still need to override a .GetHashCode ? –  Aug 11 '13 at 05:09
  • @Melina if you don't override Equals, and not going to use dictionary, list or other data structures and won't need to compare different types then you might won't need to override the two, otherwise you should, and if you override the equals, you should definitely override the hashCode – No Idea For Name Aug 11 '13 at 05:14
  • Classes in the .NET BCL rely on having consistent implementations of .Equals() and .GetHashCode() - if you don't overload both together, you can get very strange bugs. See http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx for more. – Bevan Aug 11 '13 at 05:22
1

Classes are reference types. When you create two objects and store them in variables you're only storing the reference to them. This means if you attempt to compare them you will only be comparing two references which will only be equal if they're pointing to the same object on heap. If you want to change that behavior you will have to override Equals.
Also some collections depend on GetHashCode to store elements in tree-like(or any other) structures that need some means of comparison between two objects of a given class. Which is why you need to implement these methods if you need your defined class to behave correctly under the specified circumstances.
A typical implementation of GetHashCode would be the xor of class's fields which is given in @No Idea For Name's answer. But since PersonId is unique in your example, you could also use that:

public int GetHashCode()
{
   return PersonId.GetHashCode();
}
atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • Thanks. Now I understand. As for the GetHashCode. Why is it needed? How do you recommend I should code it when my Id is unique? –  Aug 11 '13 at 04:52
  • @Melina, same reason. The default implementation of `GetHashCode` (for objects) is based on reference comparison. As a rule, `GetHashCode` should exhibit the same behavior as `Equals` (notwithstanding hash collisions). – harpo Aug 11 '13 at 04:56
  • @Melina you simply return the hash code of PersonId. And I believe I've already explained why it has to be overridden. If it's still unclear please be specific. – atoMerz Aug 11 '13 at 13:29