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.
Subscribe to:
Post Comments (Atom)
Thank you so much for clarifying this concept !!
ReplyDeleteRemember, that you can only override if the .NET method is virtual too.
ReplyDeleteactuaally why should we use overriding
ReplyDeleteif i have to redefine the method on my own then why i should override it
"actually why should we use overriding
ReplyDeleteif 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.
"actuaally why should we use overriding
ReplyDeleteif 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.
Good One
ReplyDeleteif u dont want to override and you still need same method signature u can use 'new' keyword.
ReplyDeleteactually overriding ToString() has also another benefit.
ReplyDeleteFor 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
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