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.