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.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments