How to Use the iformattable interface for formatting output

Source: Internet
Author: User
Tags first string

Analyze problems

When designing a type, programmers should try to consider it as much as possible for type users. An important point is to provide formatted string output. Write the formatting output interface for the type, and both the type and its user will get the benefit. For some types, rewrite the tostring method in the base class is enough, but for some types, you need to provide output in multiple formats. In this case, the tostring method cannot meet the requirements. In this case, you need to implement the tostring method in the iformattable:

String IFormattable.ToString(String format,IFormatProvider formatProvider)

This method accepts two parameters. The first string parameter tells the method of output format, while the second iformatprovider parameter allows users of the type to customize the formatting method. The iformatprovider interface will be discussed in subsequent sections. The following code implements the iformattable interface.

Using system; namespace test {class useiformattable: iformattable {private datetime _ time; Public useiformattable (datetime time) {_ time = time;} // rewrite tostring method public override string tostring () {return "object. tostring () ";} Public String tostring (string format, iformatprovider formatprovider) {// This section will discuss if (formatprovider! = NULL) {icustomformatter FMt = formatprovider. getformat (this. GetType () as icustomformatter; If (FMT! = NULL) {return FMT. format (format, this, formatprovider) ;}// format the output switch (Format) {Case "LD": Return _ time. tolongdatestring (); Case "LT": Return _ time. tolongtimestring (); Case "SD": Return _ time. toshortdatestring (); Case "St": Return _ time. toshorttimestring (); Case "G": // null and "" Here default: Return _ time is implemented. tostring () ;}} static void main () {useiformattable use = new useiformattable (datetime. now); // call iformattable. tostring method console. writeline (use); console. writeline (use. tostring ("LD", null); console. writeline (use. tostring ("LT", null); console. writeline (use. tostring ("SD", null); console. writeline (use. tostring ("St", null); console. read ();}}}

As shown in the code above, the iformattable. tostring method uses the first string parameter as the basis for output, and formats the type object based on different inputs. When implementing the iformattable interface, you need to pay attention to the following points:

1. The iformattable. tostring method can generally be customized to accept the format string, but three strings should be implemented: "g", null, and empty. Not implementing these three formats will not cause compilation or running errors, but may violate the. NET built-in specifications.

2. After the iformattable. tostring method is implemented, the iformattable. tostring method is called for all built-in types, instead of the tostring method inherited from system. object. In the above Code, the console. writeline method is an example.

3. The inner method automatically calls iformattable. tostring method. The parameter may be "G" or a Null String. We recommend that you convert these formats into objects during design. tostring has the same output.

4. In iformattable. at the beginning of the tostring method, the iformatprovider parameter is checked. This mechanism will be detailed in the subsequent sections. Simply put, it provides the Formatting Function for type users.

The output of the above Code is as follows:

Answer

The iformattable interface helps you to achieve a variety of formatting output types. The tostring method of iformattable accepts a string parameter that represents the format and formatted the output by analyzing this parameter. In addition, the iformattable. tostring method accepts an iformatprovider type parameter to allow users of the type to provide the formatting method.

 

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.