Why we should override the ToString method in C#

Why we should override the ToString method in C#

In this article, I am going to discuss why we should override the ToString method in C# with an example and also we will discuss how to override the ToString() method. Please read our previous article before proceeding to this article where we discussed the Properties in C# with some examples.

The System.Object class is the Superclass of all dot net types. That means, all the types in DOT NET Framework are inherited directly or indirectly from the Object class. Because of this inheritance, every type in DOT NET inherit the ToString() method from the Object class.

Consider the below example.
namespace UnderstandingObjectClassMethods
{
    public class Program
    {
        public static void Main()
        {
            int Number = 100;
            Console.WriteLine(Number.ToString());
        }
    }
}

In the above example the Number.ToString() method will give you the string representation of the integer 100. If you have a complex type let say Employee class as shown in the example below and when you call the ToString() method, then you will not get the output as expected. Hence we have to override the ToString() method, that is inherited from the Object class.

namespace UnderstandingObjectClassMethods
{
    public class Program
    {
        public static void Main()
        {
            Employee emp = new Employee();
            emp.FirstName = "Pranaya";
            emp.LastName = "Rout";
            Console.WriteLine(emp.ToString());
            Console.ReadKey();
        }
    }

    public class Employee
    {
        public string FirstName;
        public string LastName;
    }
}

When you run the above code it will give us the below output

why we should override the ToString method in C#

Our requirement is when we call the ToString() method it should display the First Name and Last Name of the Employee. To achieve this we need to Override the ToString() method which is provided by the Object class.

Modifying the ToString() Method:

Please modify the code as shown below to override the ToString() method.

namespace UnderstandingObjectClassMethods
{
    public class Program
    {
        public static void Main()
        {
            Employee emp = new Employee();
            emp.FirstName = "Pranaya";
            emp.LastName = "Rout";
            Console.WriteLine(emp.ToString());
            Console.ReadKey();
        }
    }

    public class Employee
    {
        public string FirstName;
        public string LastName;

        public override string ToString()
        {
            return FirstName + ", " + LastName;
        }
    }
}

Now run the application and you will see the First Name and Last Name of the employee as expected as shown below.

why we should override the ToString method in C#

In the next article, I am going to discuss the why we should override the equals method in C#. in this article, I try to explain why we should override the ToString method in C# with an example. I hope you understood why we need to override the ToString() method.

Leave a Reply

Your email address will not be published. Required fields are marked *