使用字符串格式化与字符串连接相比有什么好处吗?
可能的重复:
C# 字符串输出:格式化还是连接?
使用它有什么好处:
Console.WriteLine("{0}: {1}", e.Code, e.Reason);
VS。这:
Console.WriteLine(e.Code + ": " + e.Reason);
????
Possible Duplicate:
C# String output: format or concat?
what is the benefit of using this:
Console.WriteLine("{0}: {1}", e.Code, e.Reason);
VS. this:
Console.WriteLine(e.Code + ": " + e.Reason);
????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我总是使用 .Format() 的原因是为了提高可读性,从而减少错误并提高维护效率。当然,如果您对代码相对陌生,那么这毫无意义,因为第二个示例一开始更直观。第一个似乎不必要地复杂。
但是,如果您选择第二种模式,那么当您有很多变量时,您就会开始遇到问题。这是更容易理解第一种模式的好处的地方。
例如,
请注意错误:我在“文本”之后需要另一个空格,但这在本例中并不容易看到。
然而,如果我用另一种方式来做:
就会更清楚地看到发生了什么。当您将格式与将要使用的变量分开时,更容易理解最终结果。
如果我们想更长远地思考,那么它有助于全球化/定制。如果我们将这些格式字符串放入配置中,我们就可以更改变量的格式或顺序,而无需触及代码。
The reason I always use .Format() is for readability and consequently bug reduction and maintenance benefits. Of course this makes no sense if you're relatively new to code as the second example is more intuitive at first. The first appears needlessly complex.
However if you choose the second pattern then you start to have problems when you have lots of variables. This is where it is easier to appreciate the benefits of the first pattern.
e.g
Note the bug: I need another space after "text" but that's not easy to see in this example.
However if I do it the other way:
It's clearer to see what's going on. Its easier to appreciate the final result when you split the formatting from the variables that are going to be used.
If we want to think even further long term then it helps with globalisation/customisation. If we put those format strings into config we can then change the formatting or ordering of the variables without touching the code.
对我来说,
string.Format
挂件的好处是:从性能角度来看,我从未进行过任何测量;很可能连接比 string.Format 挂件更快。
For me, the benefits of the
string.Format
pendant are:From a performance perspective, I did never do any measurements; it could well be that the concatenation is faster then the
string.Format
pendant.实际上,唯一的区别是第一个允许您控制布局
或控制格式:
在紧密循环中执行大量操作时可以考虑串联的性能,在这种情况下 StringBuilder.Append 和 StringBuilder.AppendFormat 都是首选。 AppendFormat 和 string.Format 在性能方面非常接近,因此无需用第二个替换第一个。
Practically, the only difference is that the first allows you to control the layout
or control formatting:
The performance of concatenation can be considered when doing lots of it in tight loops, in which case StringBuilder.Append and StringBuilder.AppendFormat are both much preferred. AppendFormat and string.Format are very close, performance-wise, so there is no need to substitute the second for the first.
它归结为“什么时候使用 String.Format 与字符串连接更好”。
请参阅这个问题的答案:
何时使用 String.Format 与字符串连接?
It boils down to "When is it better to use String.Format vs string concatenation".
See this question for the answer:
When is it better to use String.Format vs string concatenation?
由于字符串是不可变的(不能更改现有字符串,必须每次创建一个新字符串),字符串格式可以具有性能优势,因为它不会创建尽可能多的内存引用。
<代码>
结果=字符串1+“”+字符串2+“”+字符串3
这将创建 10 个引用
结果 = string.Format("{0} {1} {2}", string1, string2, string3)
这将创建 5 个引用
As strings are immutable (cant change an existing string must create a new one each time) string format can have performance benefits as it wont create as many memory references.
result = string1 + " " + string2 + " " + string3
This creates 10 references
result = string.Format("{0} {1} {2}", string1, string2, string3)
This creates 5 references