Coding Horror

programming and human factors

Please use .ToString() responsibly

I've seen this kind of code a lot recently:

try
{
int i = 0;
int x = 0;
Console.WriteLine(i / x);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

This results in the following output:

Attempted to divide by zero.

Unless there's some compelling reason you need an ultra-terse version of the error, it's almost always better to use the provided Exception.ToString() method. Compare the difference:

System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication1.Program.Main(String[] args)
in C:Program.cs:line 15

This also brings up an interesting corollary: any object you build should have a meaningful .ToString() method. I expect a proper string representation of what you are, not a meaningless echo of your class name!

One object that violates this horribly is DataSet. Let's say we have a DataSet containing a single DataTable. When you type DataSet.ToString(), what do you think should happen? Wait, wait, don't tell me. I'll tell you. This happens:

System.Data.DataSet

Useless. How about something that actually shows the object in string form?

+----------------------------------------------------------------+
| DataSet1                                                       |
+----------------------------------------------------------------+
| Table1                                                         |
+---------+-----------+------------------+------------+----------+
| field01 | field02   | field03          | field04    | field05  |
+---------+-----------+------------------+------------+----------+
|       1 | first     | NULL             | NULL       | NULL     |
|       2 | second    | another          | 1999-10-23 | 10:30:00 |
|       3 | a third   | more foo for you | 1999-10-24 | 10:30:01 |
+---------+-----------+------------------+------------+----------+

Seems perfectly logical to me, but you'll have to write your own custom ToString() implementation to get the behavior that should have been there in the first place.

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Find me here: https://infosec.exchange/@codinghorror