1 00:00:00,650 --> 00:00:04,890 The first virtual method we'll want to know about is ToString. 2 00:00:04,890 --> 00:00:07,780 This is a very handy method for a couple of reasons. 3 00:00:07,780 --> 00:00:10,727 To get to know this method, let's go into the C# REPL. 4 00:00:12,360 --> 00:00:15,511 Let's create an empty class named Shoe. 5 00:00:18,992 --> 00:00:24,720 Because Shoe inherits from System.Object, it has the ToString method. 6 00:00:24,720 --> 00:00:27,032 ToString is an instance method, so 7 00:00:27,032 --> 00:00:32,047 we'll need to create an instance of Shoe, and now we can call ToString on it. 8 00:00:34,898 --> 00:00:39,480 ToString just returned a string that contained the name of the class. 9 00:00:39,480 --> 00:00:43,157 Let's create an instance of the System.Random class, and 10 00:00:43,157 --> 00:00:44,460 call ToString on it. 11 00:00:45,650 --> 00:00:48,260 Again, it returns the name of the class. 12 00:00:48,260 --> 00:00:51,090 The full name of the Random class is System.Random, 13 00:00:51,090 --> 00:00:53,720 because it's in the System namespace. 14 00:00:53,720 --> 00:00:57,550 The actual type name for an int is System.Int32. 15 00:00:57,550 --> 00:01:01,957 We can know that by calling GetType on an integer instance. 16 00:01:01,957 --> 00:01:05,940 Let's say 5.GetType(). 17 00:01:05,940 --> 00:01:09,735 Remember, I said that all types inherit from System.Object. 18 00:01:09,735 --> 00:01:13,050 This includes numeric types like int and double. 19 00:01:13,050 --> 00:01:17,780 This means we can call any of the methods provided by System.Object such as 20 00:01:17,780 --> 00:01:21,750 GetType and ToString on any object in C#. 21 00:01:21,750 --> 00:01:25,100 Now let's call ToString on the number 5 and see what we get. 22 00:01:28,150 --> 00:01:30,200 We get the string 5. 23 00:01:30,200 --> 00:01:34,130 You might have expected us to get System.Int32 as our string, 24 00:01:34,130 --> 00:01:37,990 just like we did with the Random class and the Shoe class. 25 00:01:37,990 --> 00:01:42,030 The reason we didn't is because the integer type 26 00:01:42,030 --> 00:01:47,080 has overridden the ToString method and provided its own implementation. 27 00:01:47,080 --> 00:01:49,060 Instead of returning System.Int32, 28 00:01:49,060 --> 00:01:54,600 it converts the value stored in the object to a string and returns that. 29 00:01:54,600 --> 00:01:58,450 Being able to override ToString is very beneficial. 30 00:01:58,450 --> 00:02:03,040 For example, the concatenation operator provided by the String class calls 31 00:02:03,040 --> 00:02:04,970 ToString on its operands. 32 00:02:04,970 --> 00:02:10,410 If we took a string and concatenated it with an integer, 33 00:02:10,410 --> 00:02:14,078 the concatenation operator here, which is this plus, 34 00:02:14,078 --> 00:02:18,320 first calls ToString on the integer 5 and 35 00:02:18,320 --> 00:02:24,640 then appends the resulting string to the end of A, and we get A5. 36 00:02:24,640 --> 00:02:29,500 Because all types inherit from System.Object and therefore all have 37 00:02:29,500 --> 00:02:34,860 a ToString method, the concatenation operator can work with any object. 38 00:02:34,860 --> 00:02:37,910 Let's use this feature to improve the Treehouse Defense game. 39 00:02:39,800 --> 00:02:42,350 In the constructor of MapLocation, 40 00:02:42,350 --> 00:02:45,840 we do this check to see if the coordinates are on the map. 41 00:02:45,840 --> 00:02:49,130 If they aren't, we throw this OutOfBoundsException. 42 00:02:49,130 --> 00:02:53,403 In the exception message, we have this x +, 43 00:02:53,403 --> 00:02:57,571 + y + is outside the boundaries of the map. 44 00:02:57,571 --> 00:03:02,412 It seems to me that we can streamline this a bit so that this just says, 45 00:03:02,412 --> 00:03:06,060 this is outside the boundaries of the map. 46 00:03:06,060 --> 00:03:10,480 Now, over in the Point class, we can override the ToString method. 47 00:03:12,070 --> 00:03:15,963 So we'll say public 48 00:03:15,963 --> 00:03:21,463 override string ToString. 49 00:03:21,463 --> 00:03:25,734 And in here, we'll 50 00:03:25,734 --> 00:03:30,763 just return x +, + y. 51 00:03:30,763 --> 00:03:36,760 Now over in the tower's FireOnInvaders method, 52 00:03:36,760 --> 00:03:41,043 let's change this message to say, 53 00:03:41,043 --> 00:03:46,908 Neutralized an invader at invader.Location. 54 00:03:51,755 --> 00:03:54,629 Now let's compile this to see how the output changes. 55 00:04:02,045 --> 00:04:03,126 And run it. 56 00:04:05,680 --> 00:04:10,000 See, here now it says, neutralized an invader at 2,2. 57 00:04:10,000 --> 00:04:15,180 Now we don't have to write x,y everywhere that we want to print a location. 58 00:04:15,180 --> 00:04:17,450 And the code is more succinct and readable. 59 00:04:20,300 --> 00:04:23,490 This works even though the concatenation operator doesn't know about our 60 00:04:23,490 --> 00:04:24,610 Point class. 61 00:04:24,610 --> 00:04:26,490 That class we wrote ourselves. 62 00:04:26,490 --> 00:04:29,440 All it knows about is System.Object. 63 00:04:29,440 --> 00:04:30,800 There are lots of methods and 64 00:04:30,800 --> 00:04:34,040 operators that need a string representation of an object. 65 00:04:34,040 --> 00:04:37,626 Overriding System.Object's ToString method allows us to 66 00:04:37,626 --> 00:04:39,670 provide whatever string we want.