[CSharp] Composite Formatting)

Source: Internet
Author: User

Composite formatting provides convenience for us to format strings, but we still need to pay attention to the following points during usage:

  • Placeholders can be ignored.
  • Enable placeholder Index exchange
  • The same placeholder can appear multiple times
  • Each placeholder must correspond to a parameter. Otherwise, a FormatException is thrown.
  • The undefined placeholder will also cause a FormatException.

Test code:

Using system; namespace systemexample {class program {static void main (string [] ARGs) {// call sample fun essential_csharp_sample_1_6 (); // readkey # If debug console. readkey (); # endif} // <summary> // composite formatting // </Summary> static void essential_csharp_sample_1_6 () {console. writeline ("{0}", "A"); // ignore the placeholder console. writeline ("", "A"); console. writeline ("{0} {1}", "A", "B"); // exchange the placeholder index console. writeline ("{1} {0}", "A", "B"); // The Console appears multiple times with the same placeholder. writeline ("{0} {0}", "A"); try {// each placeholder must correspond to a parameter; otherwise, the formatexception exception console is thrown. writeline ("{0} {1}", "A");} catch (formatexception e) {console. writeline (E. message);} Try {// undefined placeholder, it will also cause a formatexception console. writeline ("{0} {0} {A}", "A");} catch (formatexception e) {console. writeline (E. message );}}}}

Running result:

The AABBAAA index (starting from scratch) must be greater than or equal to zero and smaller than the size of the parameter list. The format of the input string is incorrect.

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.