BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

Overriding the ToString Method

Every class and object includes the ToString method. By default, this returns a string containing the name of your custom class or structure. By overriding this method, you can return more meaningful, human-readable information about your objects.

The ToString Method

The ToString method is defined in the System.Object class. Its purpose is to return a human-readable string representation of an object's contents. All classes and structures are based upon the Object class, so every such item that you define automatically inherits the ToString method.

The base class version of the ToString method simply returns the name of the object's class. For example, for the Object class the return value is "System.Object". Although this can be useful, it does not convey any information about the contents of the object's properties. To provide more appropriate details to your classes' clients, you should override the method.

The information that you return from your overridden version of ToString can be used for many purposes where human-readable text is desirable. One such use is within the Visual Studio debugger. When the debugger shows details of local or watched variables, the first information displayed is generated by calling the ToString method.

It is important to carefully consider the information that you include in the string representation of an object. You should not return anything that untrusted code could use maliciously. You should not include passwords or other confidential information.

Overriding ToString

The ToString method can be overridden using standard syntax. The method must return a string and should include important information about the underlying object. To demonstrate, create a new console application and add the following class, which represents web site information:

class WebSite
{
    private string _name;
    private string _url;

    public string Name { get { return _name; } set { _name = value; } }
    public string Url { get { return _url; } set { _url = value; } }
}

We can create a new instance of the WebSite class in the Main method of the program and set its properties. However, if we write the object to the console, only the name of the class is outputted. NB: The ToString method is used implicitly by the Console.WriteLine method.

WebSite web = new WebSite();
web.Name = "BlackWasp";
web.Url = "http://www.blackwasp.co.uk/";
Console.WriteLine(web);

/* OUTPUT

ConsoleApplication1.WebSite

*/

To improve the output, we can override the ToString method of the WebSite class. By adding the code below to the WebSite class, the name and URL of the object are combined before being returned as a string.

public override string ToString()
{
    return _name + " - " + _url;
}

Executing the program again gives improved output:

BlackWasp - http://www.blackwasp.co.uk/
15 August 2008