Format String for Type output

Source: Internet
Author: User

I have read this suggestion twice. 1. Implement the IFormattable interface to implement the ToString () Output Format String. Generally, we provide the format string output for the type by rewriting ToString (), however, this method provides a very single string output, so we can implement the ToString method of the IFormattable interface, so that the type can be formatted and output according to the user input, because the rewritten ToString method does not have any parameters, the ToString method implementing the IFormattable interface has parameters, but the code is the clearest. Copy the public class Person: IFormattable {public string FirstName {get; set;} public string LastName {get; set ;} // rewrite the ToString method to output a single public override string ToString () {return string. format ("{0}, {1}", FirstName, LastName);} // The ToString method that implements the IFormattable interface because there are parameters, therefore, you can implement complicated logic public string ToString (string format, IFormatProvider formatProvider) {if (format = "ch") return string. format ("Chinese Name: {0}, {1} ", FirstName, LastName); else return string. format ("EnglishName: {0}, {1}", FirstName, LastName) ;}} copy the code and call it like this: Person p = new Person () {FirstName = "wayne ", lastName = "chan"}; Response. write (p. toString (); Response. write (p. toString ("ch", null); Response. write (p. toString ("english", null); 2. When the formatter needs to format string output for the expected type, the IFormattable interface is implemented for the type in advance, if the type itself does not provide the function of formatting string output, then the "formatter" will be used. Copy the code // class PersonFormatter for Person: IFormatProvider, ICustomFormatter {// public matprovider member public object GetFormat (Type formatType) {if (formatType = typeof (ICustomFormatter )) return this; else return null;} // ICustomFormatter member public string Format (string format, object arg, IFormatProvider formatProvider) {Person person Person = arg as person; if (Person = null) return string. empty; swit Ch (format) {case "Ch": return string. format ("{0} {1}", person. lastName, person. firstName); case "Eg": return string. format ("{0} {1}", person. firstName, person. lastName); default: return string. format ("{0} {1}", person. firstName, person. lastName) ;}} a typical Formatter for copying code should implement the IFormatProvider and ICustomFormatter interfaces. If you use this interface, initialize a formatter as follows: person person = new Person () {FirstName = "wayne", LastName = "chan ", IDCode = "aaaa"}; // initialize the formatter PersonFormatter pFormatter = new PersonFormatter (); Response. write (pFormatter. format ("Ch", person, null); actually, I think this suggestion is very meticulous. NET knowledge. In this case, the average person will directly use the previous method. When reading a book, I also want to skip it, but finally I want, record him as well. After all, this is an improvement for yourself. Even if you forget this knowledge point in the future, you can still retrieve it from this blog.

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.