积极答复者
请问大家,是否小批量的字符串拼接就用 + 就可以了,用 StringBuilder 反而性能降低?

问题
-
请问大家,是否小批量的字符串拼接就用 + 就可以了,用 StringBuilder 反而性能降低?
如果是的话,那具体多少字符串拼接用 StringBuilder 呢?
比如下面这个情况,最多只有不超过 10 个条件的拼接,请问用 string 的 + 来拼接好,还是用 StringBuilder 好?
class Program { static void Main(string[] args) { SearchUserInfo searchInfo = new SearchUserInfo() { condition1 = "a", condition4 = "b", condition10 = "c" }; string sqlConditions = GetSqlQuery(searchInfo); Console.WriteLine(sqlConditions); } /// <summary> /// 利用搜索条件类,得到 Sql 的 where 条件 /// </summary> /// <param name="searchInfo">搜索条件类</param> /// <returns></returns> static string GetSqlQuery(SearchUserInfo searchInfo) { if (searchInfo == null) { throw new ArgumentNullException(); } StringBuilder sqlAppender = new StringBuilder(); if (!string.IsNullOrEmpty(searchInfo.condition1)) { sqlAppender.Append(" and name=" + searchInfo.condition1); } // .....下面依次是剩余的 9 个条件判断 // ..... // ..... if (!string.IsNullOrEmpty(searchInfo.condition10)) { sqlAppender.Append(" and name=" + searchInfo.condition10); } return sqlAppender.ToString(); } } class SearchUserInfo { public string condition1 { get; set; } public string condition2 { get; set; } public string condition3 { get; set; } public string condition4 { get; set; } public string condition5 { get; set; } public string condition6 { get; set; } public string condition7 { get; set; } public string condition8 { get; set; } public string condition9 { get; set; } public string condition10 { get; set; } }
无
答案
-
如果待连接的字符串在6个以下,可以使用 + 连接
如果待连接字符串在6个以上,使用 StringBuilder 比较合适。
参照:http://www.si9o.com/ct/201201/413863594.htm
http://blog.csdn.net/zx13525079024
- 已建议为答案 ThankfulHeartModerator 2012年6月12日 4:44
- 已标记为答案 陈书函 2012年6月12日 6:38
-
- 已编辑 ThankfulHeartModerator 2012年6月12日 4:44
- 已标记为答案 陈书函 2012年6月12日 6:38
全部回复
-
如果待连接的字符串在6个以下,可以使用 + 连接
如果待连接字符串在6个以上,使用 StringBuilder 比较合适。
参照:http://www.si9o.com/ct/201201/413863594.htm
http://blog.csdn.net/zx13525079024
- 已建议为答案 ThankfulHeartModerator 2012年6月12日 4:44
- 已标记为答案 陈书函 2012年6月12日 6:38
-
- 已编辑 ThankfulHeartModerator 2012年6月12日 4:44
- 已标记为答案 陈书函 2012年6月12日 6:38