Lyrk Lyrk - 1 year ago 43
C# Question

What is "4" doing in below code piece?

I understand

{0}
is the 1st argument as
id
,
{1}
is the second argument as
sw.Elapsed.TotalMilliseconds
but what is
4
for?

Console.WriteLine("End counting {0} : {1, 4:N0} ms", id, sw.Elapsed.TotalMilliseconds);

Answer Source

Here 4 is alignment specifier. It specifies to align the number 4 units right.

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.`

From String.Format Method - MSDN

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download