stringbuilder versus string concat

后端 未结 6 701
醉梦人生
醉梦人生 2020-12-01 11:14

In my project I am looping across a dataview result.

 string html =string.empty;
 DataView dV = data.DefaultView;
 for(int i=0;i< dV.Count;i++)
 {
     Da         


        
6条回答
  •  清歌不尽
    2020-12-01 11:52

    From the Documentation:

    The String class is preferable for a concatenation operation if a fixed number of String objects are concatenated. In that case, the individual concatenation operations might even be combined into a single operation by the compiler.

    A StringBuilder object is preferable for a concatenation operation if an arbitrary number of strings are concatenated; for example, if a loop concatenates a random number of strings of user input.

    So in your case i would say the String is better.

    EDIT:

    This is a no end disscussion, anyway i would recommend you to check how many opaeration do you have in average and test the performance for each one of them to compare results.

    Check this nice link regarding this issue including some performance test code.

提交回复
热议问题