Posted By

mark4asp on 10/17/10


Tagged

c#


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Override GetHashCode(), Equals() methods on a class.


 / Published in: C#
 

Why? So that your class can be used with collection classes.

// basic class

Every object has a GetHashCode() and Equals() method by default which it inherits from Object. Unfortunately these methods are useless as they stand because they say nothing specific about the class. As is, two objects of type MyStuff will only ever be equal if they are the same object (point to the same reference), making them of no use with collections.

// useful class

Add GetHashCode(), Equals() methods so that you can use the class with the Contains() method and other methods of IEnumerable collections.

I stole this from Jonathan McCracken: Test-Drive ASP.NET MCV

  1. // basic class
  2. public class MyStuff {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public int MyValue { get; set; }
  6. }
  7.  
  8. // useful class
  9. public class MyStuff {
  10. public int Id { get; set; }
  11. public string Name { get; set; }
  12. public int MyValue { get; set; }
  13.  
  14. public override int GetHashCode(){
  15. return Id;
  16. }
  17.  
  18. public override bool Equals(object obj){
  19. if (ReferenceEquals(this, obj)) return true;
  20. if (obj.GetType() != typeof (MyStuff)) return false;
  21.  
  22. var other = obj as MyStuff;
  23.  
  24. return (other.Id == Id
  25. && other.MyValue == MyValue
  26. && other.Equals(other.Name, Name));
  27. // use .Equals() here to compare objects; == for Value types
  28.  
  29. // alternative weak Equals() for value objects:
  30. // return (other.MyValue == MyValue && other.Equals(other.Name, Name) );
  31. }
  32. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: mark4asp on October 17, 2010

wot - no edit feature?

I stole this from Jonathan McCracken: Test-Drive ASP.NET MCV

Posted By: mark4asp on October 17, 2010

My first snippet and probably my last.

The Comments field in the original post is marked up as text - not converted to html.

You expect me to add my own

s and 's ? Crazy. The title is capitalized making it meaningless. GetHashCode() is meaningful, GETHASHCODE() meaningless.

Whoever wrote this program should finish developing it before releasing it upon an unsuspecting world.

Posted By: mark4asp on October 17, 2010

Doesn't allow html tags in the comments - crazier!

that should be: <p>'s and <div>'s

Posted By: mark4asp on October 17, 2010

The program can't tag something as c#. Is the hash code some specials html markup? - it wasn't the last time I looked.

I found 4 bugs in this program already. Hey - developers finish what you started please.

Posted By: Travis_King on October 18, 2010

Editing and comment formating needs a lot of work. We've been slowly working our way through the code now that we've taken over the service.

I've forwarded your problems to our Devs for them to take a look at. Thanks!

You need to login to post a comment.