1 00:00:00,510 --> 00:00:04,890 So, how can we check if two different map location objects represent the same 2 00:00:04,890 --> 00:00:06,350 location on the map? 3 00:00:06,350 --> 00:00:08,345 If we go to the documentation for 4 00:00:08,345 --> 00:00:12,600 system.object we'll find a method named Equals. 5 00:00:12,600 --> 00:00:15,540 This is the method that is called to determine if two objects 6 00:00:15,540 --> 00:00:17,210 are practically equal. 7 00:00:17,210 --> 00:00:21,020 By default, it just calls the ReferenceEquals method to see if two 8 00:00:21,020 --> 00:00:25,950 objects refer to the same object but we can override this behavior. 9 00:00:25,950 --> 00:00:31,330 Let's do that in the point class and map location will inherit this new behavior. 10 00:00:31,330 --> 00:00:33,830 So we can overwrite equals to return true 11 00:00:33,830 --> 00:00:36,700 if the coordinates of the two points are equal. 12 00:00:36,700 --> 00:00:39,496 So we'll say public override. 13 00:00:42,629 --> 00:00:47,400 Equals and equals takes a parameter of type Object. 14 00:00:47,400 --> 00:00:52,815 We can type system.object or we can just type object. 15 00:00:52,815 --> 00:00:57,760 System.object is the actual name of the object class. 16 00:00:57,760 --> 00:01:02,870 Object with the lowercase o is just an alias just like int 17 00:01:02,870 --> 00:01:05,360 actually refers to System.int32. 18 00:01:05,360 --> 00:01:07,900 We'll call our parameter obj. 19 00:01:09,220 --> 00:01:13,830 The first thing we need to do is to make sure that the object passed in 20 00:01:13,830 --> 00:01:16,120 is in fact of type point. 21 00:01:16,120 --> 00:01:22,350 So we can say if(!(obj is Point)) 22 00:01:22,350 --> 00:01:28,270 then, Will return false. 23 00:01:29,670 --> 00:01:32,442 Because if the object passed in, isn't a point, 24 00:01:32,442 --> 00:01:35,483 they can't possibly equal each other, now can they? 25 00:01:35,483 --> 00:01:40,355 Using the is operator also has the nice effect of checking to make sure 26 00:01:40,355 --> 00:01:42,371 that it isn't null either. 27 00:01:42,371 --> 00:01:50,240 Because if obj is null, then this obj is point will also return false. 28 00:01:50,240 --> 00:01:54,720 Now that we know that they are the same type, we need to cast obj to a point. 29 00:01:57,560 --> 00:01:58,280 We'll call it that. 30 00:02:01,780 --> 00:02:05,035 Now we can return true if this .X. 31 00:02:05,035 --> 00:02:10,035 Equals that .X. 32 00:02:10,035 --> 00:02:12,220 And this .Y. 33 00:02:13,520 --> 00:02:14,165 equals. 34 00:02:14,165 --> 00:02:15,300 That .Y. 35 00:02:15,300 --> 00:02:19,538 Now let's go back to the Is.OnPath method and 36 00:02:19,538 --> 00:02:25,526 change this from using the equality operator to using .Equals. 37 00:02:29,632 --> 00:02:35,080 We do this because we don't want to check if the objects are the exact same object. 38 00:02:35,080 --> 00:02:39,630 We just want to check to see if they are same for all intents and purposes. 39 00:02:39,630 --> 00:02:42,860 That is, they refer to the same coordinates on the map. 40 00:02:42,860 --> 00:02:47,300 Unless we really want to check if two variables refer to the exact same object, 41 00:02:47,300 --> 00:02:50,940 we should always use equals instead of the equality operator. 42 00:02:50,940 --> 00:02:55,340 The caveat to this rule is when dealing with numeric types and strings. 43 00:02:55,340 --> 00:02:58,880 In the case of strings and numeric types like int and double. 44 00:02:58,880 --> 00:03:01,700 The equality operator will always return true 45 00:03:01,700 --> 00:03:04,550 if the objects contain the same value. 46 00:03:04,550 --> 00:03:07,350 So when dealing with strings, ints, and doubles. 47 00:03:07,350 --> 00:03:09,550 We can always use the equality operator. 48 00:03:09,550 --> 00:03:13,270 And it will have the same result as if we called equals. 49 00:03:13,270 --> 00:03:17,610 Let's compile and run our program to see if the Is.OnPath method works as expected. 50 00:03:17,610 --> 00:03:18,110 Now. 51 00:03:19,950 --> 00:03:22,490 Yay! We get our message printed out so 52 00:03:22,490 --> 00:03:28,010 it works as expected, but what does this compiler warning here? 53 00:03:28,010 --> 00:03:32,760 This is telling us that if we override object.equals 54 00:03:32,760 --> 00:03:36,670 we should also override object.GetHashCode. 55 00:03:36,670 --> 00:03:39,390 We'll learn about the GetHashCode method in the next video.