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
By overriding Object.Equals we get to decide what it means for two objects to be equal to each other.
-
0:00
So, how can we check if two different map location objects represent the same
-
0:04
location on the map?
-
0:06
If we go to the documentation for
-
0:08
system.object we'll find a method named Equals.
-
0:12
This is the method that is called to determine if two objects
-
0:15
are practically equal.
-
0:17
By default, it just calls the ReferenceEquals method to see if two
-
0:21
objects refer to the same object but we can override this behavior.
-
0:25
Let's do that in the point class and map location will inherit this new behavior.
-
0:31
So we can overwrite equals to return true
-
0:33
if the coordinates of the two points are equal.
-
0:36
So we'll say public override.
-
0:42
Equals and equals takes a parameter of type Object.
-
0:47
We can type system.object or we can just type object.
-
0:52
System.object is the actual name of the object class.
-
0:57
Object with the lowercase o is just an alias just like int
-
1:02
actually refers to System.int32.
-
1:05
We'll call our parameter obj.
-
1:09
The first thing we need to do is to make sure that the object passed in
-
1:13
is in fact of type point.
-
1:16
So we can say if(!(obj is Point))
-
1:22
then, Will return false.
-
1:29
Because if the object passed in, isn't a point,
-
1:32
they can't possibly equal each other, now can they?
-
1:35
Using the is operator also has the nice effect of checking to make sure
-
1:40
that it isn't null either.
-
1:42
Because if obj is null, then this obj is point will also return false.
-
1:50
Now that we know that they are the same type, we need to cast obj to a point.
-
1:57
We'll call it that.
-
2:01
Now we can return true if this .X.
-
2:05
Equals that .X.
-
2:10
And this .Y.
-
2:13
equals.
-
2:14
That .Y.
-
2:15
Now let's go back to the Is.OnPath method and
-
2:19
change this from using the equality operator to using .Equals.
-
2:29
We do this because we don't want to check if the objects are the exact same object.
-
2:35
We just want to check to see if they are same for all intents and purposes.
-
2:39
That is, they refer to the same coordinates on the map.
-
2:42
Unless we really want to check if two variables refer to the exact same object,
-
2:47
we should always use equals instead of the equality operator.
-
2:50
The caveat to this rule is when dealing with numeric types and strings.
-
2:55
In the case of strings and numeric types like int and double.
-
2:58
The equality operator will always return true
-
3:01
if the objects contain the same value.
-
3:04
So when dealing with strings, ints, and doubles.
-
3:07
We can always use the equality operator.
-
3:09
And it will have the same result as if we called equals.
-
3:13
Let's compile and run our program to see if the Is.OnPath method works as expected.
-
3:17
Now.
-
3:19
Yay! We get our message printed out so
-
3:22
it works as expected, but what does this compiler warning here?
-
3:28
This is telling us that if we override object.equals
-
3:32
we should also override object.GetHashCode.
-
3:36
We'll learn about the GetHashCode method in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up