Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# System.Object Object.ToString

Radu - Adrian Buha
Radu - Adrian Buha
Courses Plus Student 5,535 Points

I got lost in second half of the tutorial. Can anyone better explain what Jeremy did?

I don't understand how the new overridden ToString method is connected to the invader.Location and how it prints the invader's location on the screen.

Can someone better explain what Jeremey did, please?

Many thanks in advance!

3 Answers

Steven Parker
Steven Parker
228,119 Points

The "ToString" method doesn't print to the screen itself, but when any operation tries to convert the object into a string (such as when it is used as a part of a "Console.WriteLine" argument), it renders a string containing both the X and Y values. Without the explicit method, the output would not contain any useful information.

A little experimentation might make this more clear than any explanation.

5,025 Points

Here's a copy and paste of my answer to "Totally lost". I've tried to connect the dots on how the Shoe example in the REPL translates to how it worked in the Treehouse Defense game.

Jeremy made these updates:

  1. Override of Point's ToString method to now return { X + ", " Y } ex. (2, 3) The key thing here is that a point object already had a ToString method that it inherited from the System.Object class because it's derived from that. So now whenever a ToString method is called against a Point object instead of returning a the default which is a string value of the object's name (example. Point point = new Point, point.ToString(), would have previously returned "point") Now with the overriding format it will return the point objects X and Y field values in the format we created (2, 3).

  2. Updated Tower class to include "invader.Location" in the Console.WriteLine method. The part that confused me was that we added invader.Location but how in the world does that end up calling our new override method in point to return a point? When we call invader.Location what we are really returning what a Location is. Which is a MapLocation object related to an Invader (see your Invader class code). A MapLocation object though is just a type of Point object. So to grab the invader.location value we are really grabbing a Point object.

  • So now you can think of it like this Console.WriteLine(invader.Location) is really => Console.WriteLine(Point point). What's also happening is that when we call Console.WriteLine(Point point) we are calling Console.WriteLine( ToString(Point point) ). Previously without our override method if we had just updated the Console.WriteLine code it would have printed to Console 'Point'. Now though because of our override method against the Point class it will print to console { X + ", " Y } ex. (2, 3)

Rather than diving into the technical details of what's going on, I found it helpful to think about it like this:

  • With the most recent code, try removing the ToString() override
  • Run the program again
  • You'll see that we now get an output of TreehouseDefense.MapLocation in our neutralisation message, as C# tries to convert our class into a readable string
  • Ergo... essentially the ToString() override provides a way for our class to present itself in a human-understandable fashion when asked to do so

This Microsoft Docs article kinda gave me my 'aha!' moment: