Effective C # Principle 5: Always provide ToString ()

Source: Internet
Author: User
Tags versions

One of the most used methods in the. NET world is System.Object.ToStrying (). You should write a "reasonable" class for all your clients (meaning this class should be user-friendly). Either you force the user of the class to use the properties of the class and add some reasonable, readable instructions. This is in the form of a string, a description of the class you designed, and it's easy to show your users some information about the object: Windows form, Web form, console output. These character descriptions can be used for debugging. Any of the types you write should be properly rewritten. When you design more complex types, you should be able to achieve more iformattable.tostring (). Admit this: if you don't rewrite (override) This routine method, or just write a bad one, your client will have to fix it for you.

The System.Object version of the ToString () method returns only the name of the type. There is not a lot of useful information: "Rect", "point", "Size" will not be shown to your users as you think. But that's only when you don't rewrite the ToString () method for your class. You only write once for your class, but your client will use it many times. When you design a class, add a little bit more work, and you'll be rewarded for each time you or someone else uses it.

(Nonsense!) )

============= =========

This principle does not translate, look a bit depressed. is a few rewritten versions of ToString (). and some formatted output. I don't think this book should discuss these entry-level content, so just read it again and there's no translation.

You know it's okay to rewrite it, and it's best to provide several overloaded versions. There is time to translate the other elements of this principle.

Give a personal advice, generally not on a class of ToString to provide a lot of instructions, give a name is enough, and then add an SDK help. More often, add a description of the member class later. I see a very rigorous structure on the ToString of a third-party library, all of which are followed by a description of some content and important attributes after the class name.

========================================= to complement translation:

Let's consider a simple requirement: rewrite the System.Object.ToString () method. Each type you design should rewrite the ToString () method to provide some of the most commonly used text descriptions for your type. Consider this customer class and its three members (fields) (in general, the fields in a class is translated into members, which is the concept of object-oriented design, whereas in the case of a database, it refers to a field):

public class Customer
{
  private string  _name;
  private decimal _revenue;
  private string  _contactPhone;
}

The ToString () method that inherits from System.Object by default returns "Customer." It's not going to help anyone too much. Even if ToString () is only used in debugging, it should be more flexible (sophisticated) some. Your rewritten ToString () method should return a text description, more like your users are using this class. In the customer example, this should be the name:

public override string ToString()
{
 return _name;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.