Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Im new to C# programming. Can someone please explain the following code:

Console.WriteLine( "{0}{1,10}", "Face", "Frequency" ); //Headings
Console.WriteLine( "{0,4}{1,10}",someval,anotherval);

I understand that this prints two columns of values with the headings given, and {0} refers to the first argument given. But what is the meaning of the format strings of the form {x,y} ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
294 views
Welcome To Ask or Share your Answers For Others

1 Answer

It adds padding to the left. Very useful for remembering the various string formatting patterns is the following cheat sheet:

.NET String.Format Cheat Sheet

Positive values add padding to the left, negative add padding to the right

Sample                                 Generates
String.Format("[{0, 10}]", "Foo");     [???????Foo]
String.Format("[{0, 5}]", "Foo");      [??Foo]
String.Format("[{0, -5}]", "Foo");     [Foo??]
String.Format("[{0, -10}]", "Foo");    [Foo???????]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...