Framework Design Guidelines: Overriding Object.ToString()
Continuing in our weekly blog post series that highlights a few of the new additions to the Framework Design Guidelines 2nd edition.. This content is found in the Object.ToString section of Chapter 8: Usage Guidelines. Christophe provides some great implementation tips for these guidelines.
DO try to keep the string returned from ToString short.
The debugger uses ToString to get a textual representation of an object to be shown to the developer. If the string is longer than the debugger can display (typically less than one screen length), the debugging experience is hindered.
In term of debugging experience, you should decorate your type with DebuggerDisplayAttribute in addition to overriding ToString for that particular purpose.
DO string formatting based on the current thread culture when returning culture-dependent information.
To be more explicit, use the CultureInfo instance returned by a thread’s CurrentCulture property to format any numeric or date, and the one returned by CurrentUICulture to look up any resource. People are often confused between the two properties.
Comments
Anonymous
February 02, 2009
Continuing in our weekly blog post series that highlights a few of the new additions to the FrameworkAnonymous
February 03, 2009
Thank you for submitting this cool story - Trackback from DotNetShoutoutAnonymous
February 03, 2009
Isn't the fact that people are often confused between the CurrentCulture and CurrentUICulture (myself included) property a sign of a design flaw in the framework?Anonymous
February 05, 2009
The comment has been removed