小编Rob*_*ing的帖子

带有"+"的字符串格式和构造字符串

我想问一下人们对于编写字符串的想法,以及构建字符串时性能是否存在巨大差异.

近年来我一直被告知永远不要做以下事情:

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText = "I am a Dog and my name is " + dogName + " and i was born on the " + bornDate.ToString("dd/MM/yyyy");
Run Code Online (Sandbox Code Playgroud)

并总是告诉类似下面的东西.

string dogName = "Ralph";
DateTime bornDate = DateTime.Parse("2010-01-01");

string fullText2 = String.Format("I am a Dog and my name is {0} and i was born on the {1:dd/MM/yyyy}", dogName, bornDate);
Run Code Online (Sandbox Code Playgroud)

我看到使用后面的语句的原因,但是有没有人知道第一个有什么类型的性能问题.

并使用StringBuilder对象而不是使用

string str = "Adding this comment to the string\n"
str += "Then Add …
Run Code Online (Sandbox Code Playgroud)

c# string stringbuilder

10
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

string ×1

stringbuilder ×1