What is the use of {0} in the console.writeline("Length: {0]"); in c#. and why are we using it?
Asked
Active
Viewed 2.2k times
1 Answers
7
{0}
is a format string parameter.
For example,
Console.WriteLine("Hello {0}!", "World")
will output
Hello World!
to the Console.
Note, that in your example, the code reads {0]
instead of {0}
and you are also missing an argument for the intended parameter. In my example, the string "World"
is the argument for the {0}
format parameter).
A good, introductory source of information for this is available at: http://www.blackwasp.co.uk/StringFormat.aspx . It is worth noting that
Console.WriteLine("Hello {0}!", "World");
is equivalent to
Console.WriteLine(string.Format("Hello {0}!", "World"));

John Saunders
- 160,644
- 26
- 247
- 397

smartcaveman
- 41,281
- 29
- 127
- 212