我想问一下人们对于编写字符串的想法,以及构建字符串时性能是否存在巨大差异.
近年来我一直被告知永远不要做以下事情:
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)