user544079
user544079

Reputation: 16639

using a method instead of override string ToString

Every object has its own public override string ToString(){ return string; } method. But we can achieve the same with a custom method. Then why have a separate override string method?

Upvotes: 1

Views: 414

Answers (6)

Ed Swangren
Ed Swangren

Reputation: 124732

Because it is part of the Object class' public interface. Library designers can always assume that there is a ToString() method for an object and use it if needed. For example, if you want your type to provide formatted text inside of a combobox you need only override ToString() and it will be displayed when you add your object in. The author of that control would have no reasonable way of calling your custom method. It's just a way of guaranteeing that every type can provide a string representation of itself.

Upvotes: 4

Hans Passant
Hans Passant

Reputation: 942020

There are lots of places where an object's ToString() method automatically gets called. Some of the more obvious ones are debugger watch expression, composite string formatting (String.Format and Console.Write), UI controls like ListBox. While you can certainly override the way they format and invoke your custom method, it is just easier to override ToString().

Upvotes: 1

Snowbear
Snowbear

Reputation: 17274

If you want to have some string method in your own classes but you do not need this method working on other classes, please, do not EVER use ToString() for that purpose. It will be a nightmare when you somebody will try to find why some particular ToString methods are overriden and have smth like :

override string ToString()
{
     return "Some very cryptic text"; 
}

It won't be easy to guess why cryptic text should be returned here and where it is used. So do not override ToString for production purposes. Only for debugging/testing.

Upvotes: 1

Kirk Woll
Kirk Woll

Reputation: 77576

Because it's virtual -- so even if, for example, all you know is that it's an object, you can still call .ToString() and expect your custom override to be called. For example:

object o = new MyClass();

Where MyClass is the class where you overrode .ToString(). You can call:

o.ToString()

And it will call your method. If however you defined your own method, like .MyToString() then o.MyToString() will result in a compiler error since it's not defined on object.

Upvotes: 0

Morten Mertner
Morten Mertner

Reputation: 9474

ToString is simply a built-in feature for converting an object to its string representation. It is something that every object therefore supports. You have the choice of overriding this if you want to provide a better representation that what the framework provides for you.

It is sometimes useful to keep ToString short or decorated with internal values, as the value is displayed by the debugger. In these situations it might make sense to add a different method to return a string for use in other scenarios.

Upvotes: 1

gjvdkamp
gjvdkamp

Reputation: 10516

A DataGrid for instance can assume that every object that is put into it has the ToString method, that enables it to display any class. It uses that to display it. If you override it you can control what is being returned. Same goes for debugger windows, it calls ToString to display whatver you;re looking at.

Upvotes: 0

Related Questions