What is the difference between , and + while concatenating?

前端 未结 3 801
傲寒
傲寒 2021-01-29 15:41

I have been coding with c# for a last couple of months now, but every time I concatenate I always get confused between the difference between the comma , and the pl

3条回答
  •  没有蜡笔的小新
    2021-01-29 16:20

    The comma is used as a separator while formatting strings, while the plus sign is used for normal concatenation.

    Formatting is where you have placeholders such as {0} and {1}. Other strings, separated by commas, are used to fill these placeholders. Note that the commas are not operators and are simply used to separate parameters. This outputs Hello world!:

    string greet = "Hello";
    Console.WriteLine("{0}{1}", greet, " world!");
    

    Concantenation is more straight forward. It simply combines the strings in order. Same example but with concatenation:

    string greet = "Hello";
    Console.WriteLine(greet + " world!");
    

    Edit: now that you have included the specific problem, here's what you can try:

    Console.WriteLine("Your id is: {0}" , id++);
    

提交回复
热议问题