通常的答案是,字符串连接对于4到8个字符串更有效。这取决于您阅读的博客。
不要编写测试来决定使用哪种方法。如果您不确定它是否会超出魔术限制,则只需使用StringBuilder。
运行以下代码以亲自查看结果:
const int sLen=30, Loops=5000;DateTime sTime, eTime;int i;string sSource = new String('X', sLen);string sDest = "";// // Time string concatenation.// sTime = DateTime.Now;for(i=0;i<Loops;i++) sDest += sSource;eTime = DateTime.Now;Console.WriteLine("Concatenation took " + (eTime - sTime).TotalSeconds + " seconds.");// // Time StringBuilder.// sTime = DateTime.Now;System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));for(i=0;i<Loops;i++) sb.Append(sSource);sDest = sb.ToString();eTime = DateTime.Now;Console.WriteLine("String Builder took " + (eTime - sTime).TotalSeconds + " seconds.");// // Make the console window stay open// so that you can see the results when running from the IDE.// Console.WriteLine();Console.Write("Press Enter to finish ... ");Console.Read();参考
http://support.microsoft.com/kb/306822



