c# string formatting

前端 未结 13 1538
离开以前
离开以前 2020-12-25 14:12

I m curious why would i use string formatting while i can use concatenation such as

Console.WriteLine(\"Hello {0} !\", name);

Console.WriteLine(\"Hello \"+          


        
13条回答
  •  感动是毒
    2020-12-25 14:34

    String formatting allows you to keep the format string separate, and use it where it's needed properly without having to worry about concatenation.

    string greeting = "Hello {0}!";
    Console.WriteLine(greeting, name);
    

    As for why you would use it in the exact example you gave... force of habit, really.

提交回复
热议问题