What do the curly braces mean in C# strings?

后端 未结 5 1790
情书的邮戳
情书的邮戳 2021-02-14 22:40
while (rdr.Read())
{
    Console.WriteLine(\"Product: {0,-35} Total: {1,2}\", rdr[\"ProductName\"], rdr[\"Total\"]);
}

What does {0,-35} mean in this c

5条回答
  •  隐瞒了意图╮
    2021-02-14 23:06

    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.

提交回复
热议问题