Overriding the ToString() Method in C#

String Operations

Sometimes it can be useful to override a method, be it your own or maybe even one of .NET’s methods. In this example I am going to create a simple class and then show you how to override its ToString() method.

In .NET, whenever you create a class, the ToString() method is automatically created for you internally. But most of the time it is not useful at all because all it will return is the class name. So to make it more useful we can override it and return our own value instead – for example a string representation of the object.

To start off let’s create a simple class as shown below:

using System;

namespace OverridingMethods
    public class Automobile
        #region Private Members
        private string brand;
        private string name;
        private string model;
        private int year;
        private string colour;

        #region Public Properties
        public string Brand
            get { return brand; }
            set { brand = value; }

        public string Name
            get { return name; }
            set { name = value; }

        public string Model
            get { return model; }
            set { model = value; }

        public int Year
            get { return year; }
            set { year = value; }

        public string Colour
            get { return colour; }
            set { colour = value; }

        #region Constructor
        public Automobile(string brand, string name, string model, int year, string colour)
            this.brand = brand;
            this.name = name;
            this.model = model;
            this.year = year;
            this.colour = colour;

The class represents an automobile. Now let’s initialise the class and call the ToString() method like this:

Automobile car = new Automobile("Hyundai", "Accent", "CRDi", 2005, "Silver");

This is the result we get: OverridingMethods.Automobile. Not very useful is it?

So to make the ToString() method of our class more useful let’s override it using the override keyword as shown below:

public override string ToString()
    return string.Format("{0} {1} {2} {3} {4}", colour, year, brand, name, model);

Now if we call the ToString() method we will get this: Silver 2005 Hyundai Accent CRDi. Now this makes much more sense doesn’t it?


DaveOnCSharp.com runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

0 comments… add one

Leave a Comment