Formatierende Ausgabeanweisungen in C♯
Beispiel
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( "abc", 8, 9 );
global::System.Console.WriteLine( "", 8, 9 );
global::System.Console.WriteLine( "abc {0}", 8, 9 );
global::System.Console.WriteLine( "abc {0}", 8, 9 );
global::System.Console.WriteLine( "abc {0} def {1} ghi", 8, 9 ); }}- Protokoll
abc
abc 8
abc 8
abc 8 def 9 ghiProgram.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( "{0:D2} {1:D5}", 8, 9 );
global::System.Console.WriteLine( "{0:F0} {1:F2}", 8, 9 );
global::System.Console.WriteLine( "{0:F0} {1:F2}", 8.345678, 9.345678 );
global::System.Console.WriteLine( "{0:N} {1:N}", 8.345678, 99876543.345678 ); }}- Protokoll
08 00009
8 9.00
8 9.35
8.35 99,876,543.35Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( "{0,-10} {1,7:N1}", "Alpha", 2.321 );
global::System.Console.WriteLine( "{0,-10} {1,7:N1}", "Beta", 512.321 );
global::System.Console.WriteLine( "{0,-10} {1,7:N1}", "Gamma", 12.321 ); }}global::System.Console.Out
Alpha 2.3
Beta 512.3
Gamma 12.3
Erklärung
An der Stelle einer eingeklammerten Zahl wird das entsprechende folgende Argument eingesetzt. Hinter einem Komma kann die Feldbreite und Ausrichtung festgelegt werden. Hinter einem Doppelpunkt können verschiedene Formatierungen festgelegt werden
Die Bedeutung kann man an Hand der Beispiele erraten oder die Beschreibungen unter dem Stichwort [composite formatting C#] mit Suchmaschinen lesen.