#663 – Visual Studio Debugger Will Call Your Object’s ToString Method

When you examine the value of a variable in the Visual Studio debugger, the debugger will call the object’s ToString method in order to display a single value for the object.  If the object’s type contains other members, you can typically expand the display to show the various members.

What this means for custom types is that you’ll see the results of the default implementation of ToString, which is to just display the name of the type.  We can see this by looking at a couple of Dog objects in the Locals window of the debugger.  Note that the “value” of each is just listed as DogLibrary.Dog.

However, if we implement the Dog.ToString method and have it dump out the dog’s name and age, we’ll now see that information in the debugger.

This demonstrates why it’s often useful, for debugging purposes, to implement ToString.