String output: format or concat in C#?

前端 未结 30 1666
一生所求
一生所求 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 11:52

    A better test would be to watch your memory using Perfmon and the CLR memory counters. My understanding is that the whole reason you want to use String.Format instead of just concatenating strings is, since strings are immutable, you are unnecessarily burdening the garbage collector with temporary strings that need to be reclaimed in the next pass.

    StringBuilder and String.Format, although potentially slower, are more memory efficient.

    What is so bad about string concatenation?

提交回复
热议问题