while (rdr.Read())
{
Console.WriteLine(\"Product: {0,-35} Total: {1,2}\", rdr[\"ProductName\"], rdr[\"Total\"]);
}
What does {0,-35} mean in this c
Those brackets are placeholders in strings for values.
So, rdr["ProductName"]
will be formatted into the first brackets of the string. And rdr["Total"]
will be formatted in the second brackets of the string.
Provided this:
rdr["ProductName"] = "My Product";
rdr["Total"] = 2.98;
Then you will output to the console:
Product: My Product Total: 2.98
After question update:
The {0,-35}
part if for alignment purpose. More information on formatting and alignment on C#'s official documentation.
it's for Align String with Spaces
To align string to the right or to the left use static method String.Format. To align string to the left (spaces on the right) use formatting patern with comma (,) followed by a negative number of characters: String.Format(„{0,–10}“, text). To right alignment use a positive number: {0,10}.
have a look at
http://www.csharp-examples.net/align-string-with-spaces/
A more simple line would be:
Console.WriteLine("{0}", 5);
The function accepts any number of arguments. They will be inserted into the string at the corresponding index. In this case, index zero holds the integer 5. The result is the string "5".
Now, you have the option the specify a format string as well as an index. Like so:
Console.WriteLine("{0:0.00}", 5);
This formats the 5
with 0.00
, resulting in 5.00
.
Thats the case for numbers, but I think those are more easy to explain. For strings, the "format" implies alignment. Also note that you use a comma rather than a colon to separate index and format.
alignment (optional): This represent the minimal length of the string. Postive values, the string argument will be right justified and if the string is not long enough, the string will be padded with spaces on the left. Negative values, the string argument will be left justied and if the string is not long enough, the string will be padded with spaces on the right. If this value was not specified, we will default to the length of the string argument.
So in your example:
{0,-35}
means string has to be at least 35 characters, leftjustified (space padding on the end).{1,2}
means string has to be at least 2 characters, rightjustified (space padding in front).I recommend this article, as well as the string.Format documentation.
Strings like "Product: {0,-35} Total: {1,2}"
are called composite format
strings.
The first numbers inside the curly braces (which start from zero) are called format items and correspond to the position of the arguments that come after the composite format string. These numbers can optionally be followed by a comma (,) and a minimum width
to apply.
The minimum width is useful for aligning columns. If the value is negative, the result will be left-aligned; otherwise, it will be right-aligned. For example:
Console.WriteLine("Product: {0,-35} Total: {1,2}", "1stProduct", 99);
Console.WriteLine("Product: {0,-35} Total: {1,2}", "SecondProduct", 111);
Results in:
Product: 1stProduct Total: 99
Product: SecondProduct Total: 111
You can see that because we have specified a minimum width of 35
characters for the product names, they will always occupy at least that much space in the result string regardless of their actual length(which were 10 and 13 in the above example, respectively). And because we have specified -35
(negative), product names will be left-aligned.
Those {}
brackets are for string formatting purpose. example, your case rdr["ProductName"]
have given a format specifier {0,-35} ... which will be left aligned with space padded to the right.