String output: format or concat in C#?

前端 未结 30 1935
一生所求
一生所求 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

    Personally, the second one as everything you are using is in the direct order it will be output in. Whereas with the first you have to match up the {0} and {1} with the proper var, which is easy to mess up.

    At least it's not as bad as the C++ sprintf where if you get the variable type wrong the whole thing will blow up.

    Also, since the second is all inline and it doesn't have to do any searching and replacing for all the {0} things, the latter should be faster... though I don't know for sure.

提交回复
热议问题