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! While you're at it, check out some resources Treehouse students have shared here.

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

christine smith
christine smith
5,959 Points

Totally lost

I have literally no idea why invader.Location would call the ToString method? Can anyone explain this better?

4 Answers

evanpavan
evanpavan
5,025 Points

Bjorn's answer helped me. Here's me trying to break it down further.

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 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 is 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)

*Overall the issue with some of the C# lessons is that the pace at which nuanced concepts are introduced is the same as very straightforward ones. Sure we just updated 3 lines of code and it compiled but why it worked was only briefly touched using the simplest case of the Shoe example. We never connected how the Shoe example translated back to how it worked in the Treehouse Defense game.

Well, because the call of invader.Location creates an object of the class MapLocation. Think of it like this: invader.Location = new MapLocation(x, y, map)

And as Jeremy said in this course, all objects inherits from the class Object. ToString() is a method of the class Object and so all objects by inherit have the method ToString().

When you call an object within WriteLine, it will inherit from the class Object and call ToString() to return the string value of the object.

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:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-override-the-tostring-method

Damir Paulić
Damir Paulić
6,591 Points

Jeremy explained it in intro video of this course.

Because if you use concatenation for example: "abc" + 5, even though 5 is not a string, it will automatically call ToString() method on int type and you get "abc5" string.

It is same here. In using concatenation with MapLocation object it automatically calls its ToString() method.