Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How should ToString() be implemented? [closed]

Tags:

c#

tostring

The problems are:

  • GUI libraries like to use ToString as a default representation for classes. There it needs to be localized.
  • ToString is used for logging. There it should provide programming related information, is not translated and includes internal states like surrogate keys and enum values.
  • ToString is used by many string operations which take objects as arguments, for instance String.Format, when writing to streams. Depending on the context you expect something different.
  • ToString is too limited if there are many different representations of the same object, eg. a long and a short form.

Because of the different usages, there are many different kinds of implementation. So they are too unreliable to be really useful.

How should ToString be implemented to be useful? When should ToString be used, when should it be avoided?


The .NET Framework documentation says:

This method returns a human-readable string that is culture-sensitive.

There is a similar question, but not the same.

like image 207
Stefan Steinegger Avatar asked Sep 10 '09 09:09

Stefan Steinegger


3 Answers

It seems you have great expectations from a tiny little method :) As far as I know it's not a good idea to use a general method in so many different contexts specially when its behavior can differ from class to class.

Here is my suggestions:

1.Do not let GUI libraries use ToString() of your objects.Instead use more meaningful properties (Almost all controls can be customized to show other properties than ToString) for example use DisplayMember. 2.When getting some information about an object (for logging or other usages) let somebody decide (another object or the object itself)what should be provided and how it should be displayed.(A strategy pattern may come in handy)

like image 178
Beatles1692 Avatar answered Oct 16 '22 15:10

Beatles1692


Here is a nice article which explains Overriding System.Object.ToString() and Implementing IFormattable

like image 44
Sauron Avatar answered Oct 16 '22 15:10

Sauron


It depends on the indended usage of your class. Many classes don't have a natural string representation (i.e. a Form object). Then I would implement ToString as an informative method (Form text, size, and so on) useful when debugging. If the class is meant to give information to the user then I would implement ToString as a default representation of the value. If you have a Vector object for instance, then ToString might return the vector as an X and Y coordinate. Here I would also add alternative methods if there are other ways to describe the class. So for the Vector I might add a method that returns a description as an angle and a lenght.

For debugging purposes you may also want to add the DebuggerDisplay attribute to your class. This tells how to display the class in the debugger, but it doesn't affect the string representation.

You may also want to consider making the value returned by ToString to be parseable so that you can create an object from a string representation. Like you can do with the Int32.Parse method.

like image 1
Rune Grimstad Avatar answered Oct 16 '22 16:10

Rune Grimstad