Hi,

I have seen in couple of places the code where ToString() method has been overriden. Like :

class Employee : IComparable<Employee>
{
public int Salary { get; set; }
public string Name { get; set; }

public int CompareTo(Employee other)
{
if (this.Salary == other.Salary)
{
return this.Name.CompareTo(other.Name);
}
return other.Salary.CompareTo(this.Salary);
}

public override string ToString()
{
// String representation.
return this.Salary.ToString() + "," + this.Name;
}
}

What is the need of overriding. I have seen if we dont override we do not get the value instead we get the object only. Why so ?

Plz reply if you have any idea.

Thanks.
Regards

toString is a method from the Object class. every class inherits at some point of the Object class.

If you do not over write the method in your new class, then it moves on to the next definition available... in the events that your class does not extend anything or that the class it extends does not overwrite toString either, then the original method from object will eventually be used.

here is a little sample project to demonstrate this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace toStringTests
{
    class Program
    {
        static void Main(string[] args)
        {
            Object o = new object();
            StringOverrider so = new StringOverrider("some string");
            ExtendingClass ec = new ExtendingClass(7, "some other string");

            Console.WriteLine("Object :\n" + o.ToString());
            Console.WriteLine("\nStringOverrider :\n" + so.ToString());
            Console.WriteLine("\nExtendingClass :\n" + ec.ToString());
            Console.ReadLine();
        }
    }

    class StringOverrider 
    {
        string myString;

        public StringOverrider(String s)
        {
            myString = s;
        }
        public override string ToString()
        {
            return "my overrided string (" + myString + "): " + base.ToString();
        }
    }

    class ExtendingClass : StringOverrider
    {
        int myInt;

        public ExtendingClass(int i , String s):base(s) 
        {
            myInt = i;
        }
        public override string ToString()
        {
            return "my second overrided string (" + myInt + "): " + base.ToString();
        }
    }
}

I override it to also create the output I want from a class when I archive it.

I will then use the .ToString().GetHashCode() to override the GetHashCode() method
I will also override the .Equals() with ToString().Equals(((MyObj)obj).ToString());

Hi dude.,

Do you know System.Object is the root class of .net framework from this only all the class are derived. In System.Object ToString() method defined as abstract method because fields in object is not stable format so we must need to override ToString() method to covert object into string

commented: Try to be sure about what you are saying. -3

One important use of overriding the ToString method in your classes is for consistency in the display of information in your views. It is far easier to override the ToString method than to remember the format used in each view for your class.

Say for example that I have a class called Employee. Let's say I want my employee record to always be displayed as first name followed by a space followed by the surname. Overriding the ToString method allows it to be performed in one place, while you may have several views containing employee information, say an employee list, a sales figures list and a graphical representation of their sales figures. To be consistent I would have to apply this formatting in three places rather than the one.

commented: Well said! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.