C# 字符串的累加优化
//未优化前 运行结果为ABC var strList = new List<string>() { "A", "B", "C" }; var str1 = ""; foreach (var str in strList) { str1 += str; }
//优化后 Append运行结果是 ABC ;AppendLine运行结果是A B C var str2 = new StringBuilder(); foreach (var str in strList) { //Append 是累加 ;AppendLine是累加并且加一个空格 str2.Append(str); str2.AppendLine(str); }
//最简介代码的优化 运行结果为ABC var str5 = strList.Aggregate((a, b) => a + b);