I suggest you ...

Better ToString() Default Methods

This falls into 2 categories.

1) fix the existing objects, for example
Console.WriteLine(new int[]{1,2,3});
currently will produce: System.Int32[]
it would be better if it was
[1,2,3]

2) for new objects
Right now anonymous objects have a great ToString()
Console.WriteLine(new {name="Fred", age=4});
will produce
{ name = Fred, age = 4 }
I would argue this should be the same by default for a Person Class
Console.WriteLine(new Person{name="Fred", age=4});
however, currently this will produce:
Namespace.Person

101 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
llewellyn falco shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

Thanks for taking the time to share this suggestion. This item has been around for a couple of versions of Visual Studio and we haven’t acted on it. Looking at the VS “15” plans, we’re not going to take action on this item, so we’re going to close it. If the suggestion is still relevant, please either take a look to see if there’s another suggestion that’s similar that you can vote on, or open a new suggestion.

- The Visual Studio Team

6 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Matthew Haugen commented  ·   ·  Flag as inappropriate

    While I certainly agree that it's useless in most cases, this change will never be made. It's just too significant. C# has been around for long enough that someone, somewhere, depends on this implementation of ToString and switching it to a "more useful" alternative would present a breaking change. If you're desperate, use @Yusak Sertiawan's suggestion of an extension method, or better yet write an intermediate class that rests between any you make and System.Object that overrides ToString and throws in some reflection for you. But don't expect a change like this to occur in the .NET Framework, at least until another, separate language is added.

  • Clifford Nelson commented  ·   ·  Flag as inappropriate

    On 1) I think you still need to tell what the base class is, but then can add the current values if they are not too long. At least include the length of the array, but better to also include members, maybe the first few and then ellipses.

  • Anonymous commented  ·   ·  Flag as inappropriate

    Python and Ruby have helper methods to return more useful information from a ToString(), some .NET libraries have this too, why not improve the default behavior as well?

Feedback and Knowledge Base