Object.GetHashCode5:05 with Jeremy McLain
When overriding Object.Equals we should also override Object.GetHashCode. Here's why and how.
In the previous video, 0:00 we got this compiler warning when we overrode object.equals in the Point class. 0:01 TreehouseDefense.Point overrides Object.Equals but 0:06 it does not override Object.GetHashCode. 0:11 The hash code of an object is an integer that is unique to that object. 0:15 Different objects will have different hash codes. 0:19 Every object created is given a unique identifier. 0:22 So by default, this ID is used as an object's hash code. 0:24 This ID, if you will, is actually just its memory address. 0:29 Being able to identify an object by its hash code is very handy and 0:33 the GetHashCode method is used by many C# collection types. 0:38 An array is just one type of collection. 0:42 There are other types of collection classes that rely on being able to 0:45 identify an object using just an integer. 0:48 The most common collection that uses the GetHashCode method is .NET framework's 0:51 dictionary class. 0:55 A hash code is only an attempt to create a unique identifier for an object. 0:57 It is possible for two different objects to have the same hash code. 1:02 Just like my name is Jeremy but there are also other people named Jeremy. 1:06 In fact it's a rather common name. 1:11 If someone called out my name in a large crowd, 1:13 there's a good chance that more than one person would respond. 1:16 How could they know which person is the Jeremy they're looking for? 1:19 Well, they'd have to rely on more than just our name. 1:23 They'd probably need to get a look at each of us 1:27 in order to say which person they wanted. 1:30 This is why the methods Equals and GetHashCode go hand in hand. 1:32 If two objects map to the same hash code, 1:37 the only way to tell them apart is to check if they're equal. 1:40 Had we only overridden the GetHashCode method, 1:44 we'd get a similar compiler warning asking us to override Equals too. 1:47 Let's get a better idea of how the GetHashCode method works 1:52 by overriding it in Point. 1:55 So we'll type public 2:00 override int GetHashCode. 2:04 In order to implement GetHashCode, we need to make a couple decisions. 2:10 First if two objects are equal as we've defined equality to be up 2:14 here should their hash codes also be equal? 2:18 We almost always want this to be the case. 2:21 And this is what users of the GetHashCode method will expect. 2:24 Here we see that we determine if the Point objects are equal by comparing the X and 2:28 Y coordinates with each other. 2:34 We should also use X and Y when creating the new hash code. 2:35 But here's the problem that we always run into when creating hash codes. 2:39 We can only return a single integer as the hash code. 2:44 Here we have two integers, X and Y. 2:48 We can't return them both. 2:50 Let's say we just returned X as the hash code. 2:52 That means that all points with the same X value would have the same hash code. 2:55 That isn't very unique. 3:00 Our goal is to create a hash code that is unique as possible. 3:02 The best chance we have of doing this is by combining X and 3:06 Y, somehow, into a single integer. 3:09 There are lots of ways we could do this. 3:13 We could just add them together. 3:15 If X was five and Y was two we'd end up with a hash code of seven. 3:17 There are lots of ways to add two different integers together to get seven. 3:24 All of those points would also have the same hash code. 3:27 We could reduce those combinations by half just by multiplying X or 3:31 Y by a prime number before adding them together. 3:36 By doing this we're ensuring that X equals 5, and Y equals 2 3:39 gives us a different hash code than X equals 2 and Y equals 5. 3:44 So here we'll say return x 3:49 Times a relatively small prime number 31, and then add Y. 3:56 There are lots of ways to create hash codes, and 4:04 they are all compromises of some sort. 4:07 For what we're doing this is probably good enough. 4:10 I should mention that when computing hash codes it's best to compute them from 4:13 the hash codes of the fields that we're combining together. 4:16 This is because their hash codes are already fairly unique. 4:20 So we should write this as X.GetHashCode * 31 + Y.GetHashCode. 4:24 In the case of integers the result is the same since 4:31 the hash code of an integer is just its value. 4:37 So the hash code of five would be five. 4:44 If X and Y were of any other type it 4:47 would be important to call their GetHashCode methods. 4:49 This is just a good practice to always use GetHashCode when computing a hash code. 4:52 We'll learn more about how hash codes are used in other courses 4:58 where we use more advanced .NET collection types. 5:01
You need to sign up for Treehouse in order to download course files.Sign up