下面的代码段中“4”在做什么?
What is "4" doing in below code piece?
我理解 {0}
是第一个参数 id
,{1}
是第二个参数 sw.Elapsed.TotalMilliseconds
但是 4
是什么?
Console.WriteLine("End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
这里的4
是对齐说明符。它指定将数字 4 单元右对齐。
Controlling alignment
By default, strings are right-aligned within their field if you specify a field width. To left-align strings in a field, you preface the field width with a negative sign, such as {0,-12} to define a 12-character right-aligned field.`
这是对齐方式。来自 MSDN(强调我的):
Each format item takes the following form and consists of the following components:
{index[,alignment][:formatString]}
...
The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.
我理解 {0}
是第一个参数 id
,{1}
是第二个参数 sw.Elapsed.TotalMilliseconds
但是 4
是什么?
Console.WriteLine("End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);
这里的4
是对齐说明符。它指定将数字 4 单元右对齐。
Controlling alignment
By default, strings are right-aligned within their field if you specify a field width. To left-align strings in a field, you preface the field width with a negative sign, such as {0,-12} to define a 12-character right-aligned field.`
这是对齐方式。来自 MSDN(强调我的):
Each format item takes the following form and consists of the following components:
{index[,alignment][:formatString]}
...
The optional alignment component is a signed integer indicating the preferred formatted field width. If the value of alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width. The formatted data in the field is right-aligned if alignment is positive and left-aligned if alignment is negative. If padding is necessary, white space is used. The comma is required if alignment is specified.