How should I concatenate strings?

前端 未结 9 1427
不知归路
不知归路 2020-12-06 00:43

Are there differences between these examples? Which should I use in which case?

var str1 = \"abc\" + dynamicString + dynamicString2;

var str2 = String.Form         


        
9条回答
  •  执笔经年
    2020-12-06 01:05

    My rule of thumb is to use String.Format if you are doing a relatively small amount of concatination (<100) and StringBuilder for times where the concatination is going to be large or is potentially going to be large. I use String.Join if I have an array and there isn't any formatting needed.

    You can also use the Aggregate function in LINQ if you have an enumerable collection: http://msdn.microsoft.com/en-us/library/bb548651.aspx

提交回复
热议问题