This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
HashSet relies on each item's hash code. Let's learn about hash codes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's go ahead and
change the students list back here and
0:00
main from a list to a hash set as well.
0:02
So change this list to the HashSet and
also over here.
0:05
Notice that we can initialize a hash
set the same way we did with lists.
0:12
Probably the most common way
to initialize a hash set
0:16
is by passing another collection
into its constructor.
0:19
The constructors of both list and
hash set accept any innumerable type.
0:22
So we can pass in any
collection type we want because
0:27
they all implement innumerable.
0:30
I'll change this so
that we're creating a list and
0:32
then passing it into
the hash set constructor.
0:35
So add a parenthesis here and
say new List<student>.
0:37
Now these curly braces become
the initialization list for
0:42
the list</student> and
then close the parenthesis.
0:45
So this is essentially
creates a set from the list.
0:48
It will automatically take out
any duplicate items in the list.
0:51
This can be pretty handy.
0:55
For now I'll change this back to
a simple collection initializer.
0:57
Let's play around a bit with hash sets by
working with the student set directly.
1:01
We can go ahead and delete this other
code here, And we'll change this for
1:06
each loop so that it's looping through
the students that are declared in main.
1:14
Let's create a new student and
add it to the list.
1:20
So I'll say student Joe Equals new student
1:23
with the name of Joe, and
1:30
grade level is second grade.
1:35
We can add Joe to the set
using the add method.
1:41
So say Students.Add(joe).
1:45
Now it's compile and run this.
1:50
We can see that Joe is now
part of our set of students.
1:57
We've seen what happens when we try
to add an object that's already been
2:00
added to the set.
2:04
It doesn't get added because it would be a
duplicate and sets don't allow duplicate.
2:05
Let's do something a little different.
2:11
Let's try adding Joe again.
2:13
But let's create a new object and
pass that in instead.
2:15
So here, I'll create another student
object called duplicate Joe.
2:20
And I'll just copy this
instantiation from up here.
2:28
Now I'll add duplicate
Joe to the students set.
2:35
So I'll say, students.add(duplicateJoe).
2:37
Both of these objects
contain the same information.
2:45
They both represent the student Joe.
2:48
Let's see what happened when we run this
and print out the contents of the set now.
2:51
Now it appears that we have
a duplicate student in our set
2:56
that isn't supposed to allow duplicates.
2:58
What happened?
3:01
Although, these two objects
contain the same data,
3:02
they're still two distinct objects.
3:05
HashSet uses hash codes to determine
if two objects are the same.
3:07
A hash code is an integer that can
be used to identify an object.
3:12
Every class in C# inherits
from the System.Object class
3:16
which contains a GetHashCode method.
3:20
Let's print the result of calling
3:22
GetHashCode on each of these two objects.
3:27
So here I'll say
Console.Writeline(joe.GetHashCode)..
3:32
And also print out the hash code for
duplicateJoe.
3:40
Just compile that and run them,
A little more space here,
3:48
when we run this we see that it
prints out two different hashCodes.
3:56
By default the gethashCode method returns
a unique number for every object.
4:00
If we want two objects to
return the same hash code,
4:06
we need to override
the get hash code method.
4:09
We should override the get hash code
method so that it returns the same hash
4:12
code when the properties of
the student objects are the same.
4:15
So back in student,
let's create another method and
4:18
it will override the GetHashCode method.
4:24
Now and here, we need to return a number
that unique to the contents of the object.
4:34
To do that we need to return an integer
that's a representation of the information
4:38
in the object.
4:43
In this case it's the values contained
in the name and grade level properties.
4:44
We can simplify this task by
just combining the hash codes
4:50
of these properties.
4:53
So we'll put Name.GetHashCode()
[BLANK AUDIO] and
4:56
we also have GradeLevel.GetHashcode.
5:03
The problem is these are two integers,
but we can only return one.
5:12
If we only return the hash code for
the name property then
5:16
any student object with the same
name would have the same hash code.
5:20
Identical strings have the same hash code.
5:24
We'll have a similar problem if we only
returned the hash code of the grade
5:27
level property.
5:30
The hash code of an integer is just
the value of the integer itself.
5:32
Instead, we should return an integer
that's a combination of the two hash
5:38
codes.
5:43
There are a number of
ways we could do this.
5:43
Probably the simplest is just to add the
hash code of the grade level to the hash
5:46
code of the name property like this.
5:50
This will create a sufficiently
unique hash code for student objects.
5:59
This works because the hash code for
any string is, on average,
6:02
significantly different
than any other string.
6:06
However, grade level can only take
on one of relatively few values.
6:10
So adding this to the hash code of the
name property produces a resulting hash
6:14
code that's unlikely to be the same for
any two student objects.
6:19
Had our student object
come from a database,
6:23
it would most likely have had
a unique ID assigned to it.
6:26
In that case we could return
this ID as the hash code.
6:29
The way we implement the GetHashCode
method depends on the number of types and
6:32
properties who want to
compute the hash code from.
6:36
See the teacher's notes for
6:39
some additional suggestions
on how to compute hash codes.
6:40
I suggest you take a look at those
suggestions before you attempt to write
6:43
your own GetHashCode methods.
6:46
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up