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