CLR via C # Char_String continued

Source: Internet
Author: User

// The previous editor was originally intended to be re-edited, but it was changed back to the Tiny MCE editor after I adjusted it to Markdown.

1. Format (), ToString () method & IFormattable & IFormatProvider

First, ToString (). Four ToString methods are defined in System. Int32.

1. The ToString () with no parameter is used to override the Object, (or ValueType)
2, 3, and 4 are used in the same way. The third one can customize the Provider.

Int implements the IFormattable interface.

Only one ToString method, format parameter, and provider of the IFormatProvide type are defined in IFormattable.
There are only a few types of IFormatProvider implemented in. NET, including SystemGlobalzation. CultureInfo

IFormatProvider defines a GetFormat method (type) to obtainSystem. GlobalizationIn the namespaceNumberFormatInfoAndDateTimeFormatInfoInstance,
These two classes, like CultureInfo, are also instances of the IFormatProvider, because currently. NET involves internationalization operations, numbers, and dates. numberFormatInfo and DateTimeFormatInfo. If you want to get the date, you need to format the number. the excerpt from DateTimeFormatInfo is as follows. It also includes Chinese, Japanese, and other related content.

DateTimeFormatInfo, NumberFormatInfo is an IFormatProvider instance. Their GetFormat method returns this or null Based on the type parameter.

Instance:

I = Console. writeLine (I. toString (); Console. writeLine (I. toString (); Console. writeLine (I. toString (); Console. writeLine (I. toString (); Console. writeLine (I. toString (, System. globalization. cultureInfo. invariantCulture); Console. writeLine (I. toString (, System. globalization. cultureInfo ()));}View Code

ToString (), ToString (format) are actually equivalent to calling ToString (format, provider) version,
When the first parameter is null, the default value is null or "G" (generally)
If the second miserable parameter is null, the regional cultural information of the currently called Thread is obtained based on System. Globalization. CultureInfo. CurrentCulture or System. Threading. Thread. CurrentThread. CurrentCulture.

 

2. About String. Fornat, StringBuilder. AppendFormat

String. format (format, param object [] arg), such as String. format ("The Post with id equals {0: x4} is wiite on {1}", 1, DateTime. now)

When the Format method encounters a tag such as {0}, it will call the ToString (format) of arg based on the format following the colon. At the same time, the Format overload allows the passing of a custom Provider

The following uses the example on CLR via C # To write a custom IFormatProvider. function: output the char type according to unicode

The result is displayed

Code

(FormatType = Format (format, str = (arg. GetType () = () & format! =) (Format = str = + () arg )). toString (format = str = + () arg )). toString (arg IFormattable) str = (arg IFormattable ). toString (format, (arg! = Str =}View Code

It's a bit late today. I will try again later ~~~

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.