String output: format or concat in C#?

前端 未结 30 1640
一生所求
一生所求 2020-11-22 11:40

Let\'s say that you want to output or concat strings. Which of the following styles do you prefer?

  • var p = new { FirstName = \"Bill\", LastName = \"Ga

30条回答
  •  盖世英雄少女心
    2020-11-22 12:02

    For basic string concatenation, I generally use the second style - easier to read and simpler. However, if I am doing a more complicated string combination I usually opt for String.Format.

    String.Format saves on lots of quotes and pluses...

    Console.WriteLine("User {0} accessed {1} on {2}.", user.Name, fileName, timestamp);
    vs
    Console.WriteLine("User " + user.Name + " accessed " + fileName + " on " + timestamp + ".");
    

    Only a few charicters saved, but I think, in this example, format makes it much cleaner.

提交回复
热议问题