(41)StringBuilder is NOT the answer for all string concatenation scenarios; String.Join could be

招数41:

StringBuilder不适用于所有字符串连接的场景;String.Join可能是

Yes, if you are in a loop and adding to a string, then a StringBuilder *could* be most appropriate. However, the overhead of spinning up a StringBuilder instance makes the following pretty dumb:
是的,如果你是在一个循环中并添加到一个字符串,那么StringBuilder可能是最适合的。然而,创建一个StringBuilder实例的开销让下面的代码相当愚蠢的。

var sb = new StringBuilder();
sb.Append(“Frankly, this is “);
sb.Append(notMoreEfficient);
sb.Append(“. Even if you are in a loop.”);
var whyNotJustConcat = sb.ToString();

Instead, use String.Join, which is typically more performant than spinning up a StringBuilder instance for a limited number of strings. It’s my go-to concat option:
相反,对于一个有限数量的字符串使用String.Join比创建一个StringBuilder实例通常是更具表现力的。这是我首选字符串连接方式:

string key = String.Join(“ “, new String[]
{ “This”, “is”, “a”, “much”, “better”,
solution, “.”});

The first variable of " " can just be set to "" when you don’t want a delimiter.
当你不想要一个分隔符时候,第一个变量的“ ”可以被设置为“”。

For loops that do a lot of, er, looping, sure, use a StringBuilder.
循环中那么做就多余了,嗯,循环,当然是用StringBuilder。

Just don’t assume it’s the de facto solution in all, or even the majority of cases. My rule of thumb is to add strings together when I’ve got one to five of them (likewise with String.Format if it helps with legibility). For most other cases, I tend towards String.Join. Only when dealing with a loop that isn’t limited to about 10 iterations, especially one that really lets rip, do I spin up a StringBuilder.
只要不认为他不是所有的解决方案,甚至大多数情况下。我的原则是当我有1到5个字符连接的时候(同样是String.Format如果他有助于易读性) 对于其他大多数情况下,我倾向于String.Join。只有当处理一个并不限于10次迭代的循环,特别是...,我会使用StringBuilder。

教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是的更多相关文章

  1. 教你50招提升ASP.NET性能(十三):精选技巧集合

    (19)A selection of tips 招数19: 精选技巧集合 Including height and width in <img /> tags will allow you ...

  2. 教你50招提升ASP.NET性能(二十六):对于开发人员的数据库性能技巧

    Database Performance Tips for Developers对于开发人员的数据库性能技巧 As a developer you may or may not need to go ...

  3. 教你50招提升ASP.NET性能(十六):把问题仍给硬件而不是开发人员

    (27)Throw hardware at the problem, not developers 招数27: 把问题仍给硬件而不是开发人员 As developers, we often want ...

  4. 教你50招提升ASP.NET性能(十一):避免在调试模式下运行网站

    (17)Avoid running sites in debug mode 招数17: 避免在调试模式下运行网站 When it comes to ASP.NET, one of the most c ...

  5. 教你50招提升ASP.NET性能(七):总是在服务器端执行验证

    (13)Always perform validation on the server as well 招数13: 总是在服务器端执行验证 This isn’t exactly a performan ...

  6. 教你50招提升ASP.NET性能(二):移除不用的视图引擎

    (2)Remove unused View Engines 招数2: 移除不用的视图引擎 If you're an ASP.NET MVC developer, you might not know ...

  7. 教你50招提升ASP.NET性能(二十四):ORM小窍门

    ORM TipsORM小窍门 More and more people are using Object to Relational Mapping (ORM) tools to jump the d ...

  8. 教你50招提升ASP.NET性能(二十一):避免使用会话状态

    (39)Avoid using session state 招数39: 避免使用会话状态 Where possible, you should try and avoid using session ...

  9. 教你50招提升ASP.NET性能(二十):7条便利的ViewState技巧

    (32)Seven handy ViewState tips 招数32: 7条便利的ViewState技巧 Every time I have to deal with a classic ASP.N ...

  10. 教你50招提升ASP.NET性能(十九):静态集合

    (30)Static collections 招数30: 静态集合 If a collection is static, make sure it only contains the objects ...

随机推荐

  1. python - 回溯继承树 - 自己实现

    # -*- coding: utf-8 -*- class test(object): pass class test1(test): pass class test2(test1): pass pr ...

  2. <二>面向对象分析之几个关键的概念

    一:建模        --->建模,是指通过对[客观事物]建立一种抽象的方法用以表征事物并获得对事物本身的理解.同时把这种理解概念化,将这些逻辑概念组织起来,构成一种对所观察对象的内部结构和工 ...

  3. distinct数据去重关键字

    在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 示例1 select distinct nam ...

  4. Android-setDefaultKeyMode方法

    在Activity中的onCreate()方法中使用setDefaultKeyMode()可以做到在当前activity中打开拨号器.执行快捷键.启动本地搜索.启动全局搜索: setDefaultKe ...

  5. 升级WordPress

    1. 备份文件 mv wordpress wordpress_3.6 2. 下载新版本 wget http://cn.wordpress.org/wordpress-3.8-zh_CN.zip 3. ...

  6. Asp.net 将DataTable 或者DataSet 转换为Json 格式

    Web 开发中,将从数据库中取到的数据直接转换为 Json 格式的数据,在前台通过Ajax 无刷新显示在界面上,下面提供将DataTable 或者DataSet 转换为Json 的方法 /// < ...

  7. CF 577C Vasya and Petya's Game

    题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的 ...

  8. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...

  9. UVA 12532-Interval Product(BIT)

    题意: 给n个数字的序列,C v p 把v位上的数字置成p , S l r代表求区间[l,r]各数字相乘得数的符号,对于每个S输出所得符号(’+‘,’-‘,’0‘) 分析: 开两个数组表示区间负数的个 ...

  10. 炮兵阵地(POJ 1185状压dp)

    题意:n*m地图'H'能放'p'不能放,布兵的方格上下左右不能布兵,给你地图求最大布兵数 分析:关系到前两行,所以dp[i][j][k]第i行状态为j,i-1行状态为k时的最大布兵数, 先求出所有可行 ...