String output: format or concat in C#?

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

    Nice one!

    Just added

            s.Start();
            for (var i = 0; i < n; i++)
                result = string.Concat(p.FirstName, " ", p.LastName);
            s.Stop();
            ceElapsedMilliseconds = s.ElapsedMilliseconds;
            ceElapsedTicks = s.ElapsedTicks;
            s.Reset();
    

    And it is even faster (I guess string.Concat is called in both examples, but the first one requires some sort of translation).

    1000000 x result = string.Format("{0} {1}", p.FirstName, p.LastName); took: 249ms - 3571621 ticks
    1000000 x result = (p.FirstName + " " + p.LastName); took: 65ms - 944948 ticks
    1000000 x result = string.Concat(p.FirstName, " ", p.LastName); took: 54ms - 780524 ticks
    

提交回复
热议问题