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