Why should you override the ToString() method


Why should you override the ToString() method?
All types in .Net inherit from system.object directly or indirectly. Because of this inheritance, every type in .Net inherit the ToString() method from System.Object class. Consider the example below.

using System;
public class MainClass
{
  public static void Main()
  {
   int Number = 10;
   Console.WriteLine(Number.ToString());
  }
}

In the above example Number.ToString() method will correctly give the string representaion of int 10, when you call the ToString() method.

If you have a Customer class as shown in the below example and when you call the ToString() method the output doesnot make any sense. Hence you have to override the ToString() method, that is inherited from the System.Object class.

using System;
public class Customer
{
 public string FirstName;
 public string LastName;
}
public class MainClass
{
 public static void Main()
 {
  Customer C = new Customer();
  C.FirstName = "David";
  C.LastName = "Boon";
  Console.WriteLine(C.ToString());
 }
}


The code sample below shows how to override the ToString() method in a class, that would give the output you want.


using System;
public class Customer
{
  public string FirstName;
  public string LastName;

  public override string ToString()
  {
    return LastName + ", " + FirstName;
  }
}
public class MainClass
{
  public static void Main()
  {
    Customer C = new Customer();
    C.FirstName = "David";
    C.LastName = "Boon";
    Console.WriteLine(C.ToString());
  }
}

Conclusion : If you have a class or a struct, make sure you override the inherited ToString() method.

9 comments:

  1. Thank you so much for clarifying this concept !!

    ReplyDelete
  2. Remember, that you can only override if the .NET method is virtual too.

    ReplyDelete
  3. actuaally why should we use overriding
    if i have to redefine the method on my own then why i should override it

    ReplyDelete
  4. "actually why should we use overriding
    if i have to redefine the method on my own then why i should override it"
    This is a basic concept of OOP. I believe the idea is encapsulation. If you override the ToString() method, the user can use your class object as if it is a string, by calling the regular methods he would call on a string object. Thus it hides the details of the inner functioning of the class from the user.

    ReplyDelete
  5. "actuaally why should we use overriding
    if i have to redefine the method on my own then why i should override it "

    ToString() belongs to System.Object which is the implicit base class for all objects. Therefore we need to override the method to use it.

    ReplyDelete
  6. if u dont want to override and you still need same method signature u can use 'new' keyword.

    ReplyDelete
  7. actually overriding ToString() has also another benefit.

    For value types which do not override it (.net defined value types do) this value types will be boxed
    into object which is a performance penalty and not a small one. So if you wanto to avoid boxing for your
    cistom value types override ToString

    ReplyDelete
  8. some times in real time we get null values in string field so if you use .tostring for null values you ll get compile time error

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us