Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Intermediate C#!
      
    
You have completed Intermediate C#!
Preview
    
      
  All objects can be converted to a string using ToString. We get to decide what we want that conversion to be.
This video doesn't have any notes.
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
                      The first virtual method we'll
want to know about is ToString.
                      0:00
                    
                    
                      This is a very handy method for
a couple of reasons.
                      0:04
                    
                    
                      To get to know this method,
let's go into the C# REPL.
                      0:07
                    
                    
                      Let's create an empty class named Shoe.
                      0:12
                    
                    
                      Because Shoe inherits from System.Object,
it has the ToString method.
                      0:18
                    
                    
                      ToString is an instance method, so
                      0:24
                    
                    
                      we'll need to create an instance of Shoe,
and now we can call ToString on it.
                      0:27
                    
                    
                      ToString just returned a string that
contained the name of the class.
                      0:34
                    
                    
                      Let's create an instance of
the System.Random class, and
                      0:39
                    
                    
                      call ToString on it.
                      0:43
                    
                    
                      Again, it returns the name of the class.
                      0:45
                    
                    
                      The full name of the Random
class is System.Random,
                      0:48
                    
                    
                      because it's in the System namespace.
                      0:51
                    
                    
                      The actual type name for
an int is System.Int32.
                      0:53
                    
                    
                      We can know that by calling
GetType on an integer instance.
                      0:57
                    
                    
                      Let's say 5.GetType().
                      1:01
                    
                    
                      Remember, I said that all types
inherit from System.Object.
                      1:05
                    
                    
                      This includes numeric types like int and
double.
                      1:09
                    
                    
                      This means we can call any of the methods
provided by System.Object such as
                      1:13
                    
                    
                      GetType and ToString on any object in C#.
                      1:17
                    
                    
                      Now let's call ToString on
the number 5 and see what we get.
                      1:21
                    
                    
                      We get the string 5.
                      1:28
                    
                    
                      You might have expected us to
get System.Int32 as our string,
                      1:30
                    
                    
                      just like we did with the Random class and
the Shoe class.
                      1:34
                    
                    
                      The reason we didn't is
because the integer type
                      1:37
                    
                    
                      has overridden the ToString method and
provided its own implementation.
                      1:42
                    
                    
                      Instead of returning System.Int32,
                      1:47
                    
                    
                      it converts the value stored in
the object to a string and returns that.
                      1:49
                    
                    
                      Being able to override
ToString is very beneficial.
                      1:54
                    
                    
                      For example, the concatenation operator
provided by the String class calls
                      1:58
                    
                    
                      ToString on its operands.
                      2:03
                    
                    
                      If we took a string and
concatenated it with an integer,
                      2:04
                    
                    
                      the concatenation operator here,
which is this plus,
                      2:10
                    
                    
                      first calls ToString on the integer 5 and
                      2:14
                    
                    
                      then appends the resulting string
to the end of A, and we get A5.
                      2:18
                    
                    
                      Because all types inherit from
System.Object and therefore all have
                      2:24
                    
                    
                      a ToString method, the concatenation
operator can work with any object.
                      2:29
                    
                    
                      Let's use this feature to improve
the Treehouse Defense game.
                      2:34
                    
                    
                      In the constructor of MapLocation,
                      2:39
                    
                    
                      we do this check to see if
the coordinates are on the map.
                      2:42
                    
                    
                      If they aren't,
we throw this OutOfBoundsException.
                      2:45
                    
                    
                      In the exception message,
we have this x +,
                      2:49
                    
                    
                      + y + is outside
the boundaries of the map.
                      2:53
                    
                    
                      It seems to me that we can streamline
this a bit so that this just says,
                      2:57
                    
                    
                      this is outside the boundaries of the map.
                      3:02
                    
                    
                      Now, over in the Point class,
we can override the ToString method.
                      3:06
                    
                    
                      So we'll say public
                      3:12
                    
                    
                      override string ToString.
                      3:15
                    
                    
                      And in here, we'll
                      3:21
                    
                    
                      just return x +, + y.
                      3:25
                    
                    
                      Now over in the tower's
FireOnInvaders method,
                      3:30
                    
                    
                      let's change this message to say,
                      3:36
                    
                    
                      Neutralized an invader
at invader.Location.
                      3:41
                    
                    
                      Now let's compile this to
see how the output changes.
                      3:51
                    
                    
                      And run it.
                      4:02
                    
                    
                      See, here now it says,
neutralized an invader at 2,2.
                      4:05
                    
                    
                      Now we don't have to write x,y everywhere
that we want to print a location.
                      4:10
                    
                    
                      And the code is more succinct and
readable.
                      4:15
                    
                    
                      This works even though the concatenation
operator doesn't know about our
                      4:20
                    
                    
                      Point class.
                      4:23
                    
                    
                      That class we wrote ourselves.
                      4:24
                    
                    
                      All it knows about is System.Object.
                      4:26
                    
                    
                      There are lots of methods and
                      4:29
                    
                    
                      operators that need a string
representation of an object.
                      4:30
                    
                    
                      Overriding System.Object's ToString
method allows us to
                      4:34
                    
                    
                      provide whatever string we want.
                      4:37
                    
              
        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