使用字符串格式化与字符串连接相比有什么好处吗?

发布于 2024-10-14 03:35:45 字数 368 浏览 9 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月静好 2024-10-21 03:35:45

我总是使用 .Format() 的原因是为了提高可读性,从而减少错误并提高维护效率。当然,如果您对代码相对陌生,那么这毫无意义,因为第二个示例一开始更直观。第一个似乎不必要地复杂。

但是,如果您选择第二种模式,那么当您有很多变量时,您就会开始遇到问题。这是更容易理解第一种模式的好处的地方。

例如,

Console.Writeline(var1 + " " + var2 + "-" + var3 +
 " some sort of additional text" + var4);

请注意错误:我在“文本”之后需要另一个空格,但这在本例中并不容易看到。

然而,如果我用另一种方式来做:

Console.Writeline("{0} {1}-{2} some sort of additional text{3}", 
 var1, var2, var3, var4)

就会更清楚地看到发生了什么。当您将格式与将要使用的变量分开时,更容易理解最终结果。

如果我们想更长远地思考,那么它有助于全球化/定制。如果我们将这些格式字符串放入配置中,我们就可以更改变量的格式或顺序,而无需触及代码。

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

Console.Writeline(var1 + " " + var2 + "-" + var3 +
 " some sort of additional text" + var4);

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:

Console.Writeline("{0} {1}-{2} some sort of additional text{3}", 
 var1, var2, var3, var4)

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.

风启觞 2024-10-21 03:35:45

对我来说,string.Format 挂件的好处是:

  • 提高可读性
  • 更好的可翻译性

从性能角度来看,我从未进行过任何测量;很可能连接比 string.Format 挂件更快。

For me, the benefits of the string.Format pendant are:

  • Improved readability
  • Better translatable

From a performance perspective, I did never do any measurements; it could well be that the concatenation is faster then the string.Format pendant.

〃安静 2024-10-21 03:35:45

实际上,唯一的区别是第一个允许您控制布局

string.Format("*{0:3}*",1); // *  1*

或控制格式:

string.Format("*{0:c}*",1); // *$1.00*

在紧密循环中执行大量操作时可以考虑串联的性能,在这种情况下 StringBuilder.AppendStringBuilder.AppendFormat 都是首选。 AppendFormat 和 string.Format 在性能方面非常接近,因此无需用第二个替换第一个。

Practically, the only difference is that the first allows you to control the layout

string.Format("*{0:3}*",1); // *  1*

or control formatting:

string.Format("*{0:c}*",1); // *$1.00*

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.

鸵鸟症 2024-10-21 03:35:45

它归结为“什么时候使用 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?

厌味 2024-10-21 03:35:45

由于字符串是不可变的(不能更改现有字符串,必须每次创建一个新字符串),字符串格式可以具有性能优势,因为它不会创建尽可能多的内存引用。

<代码>
结果=字符串1+“”+字符串2+“”+字符串3

这将创建 10 个引用

  • result
  • string1
  • string2
  • string3
  • " "
  • " "
  • string1 + " "
  • string1 +
  • " " + string2 string1 + " " + string2 + " "
  • string1 + " " + string2 + " " + string 3


结果 = string.Format("{0} {1} {2}", string1, string2, string3)

这将创建 5 个引用

  • result
  • string1
  • string2
  • string3
  • "{0} {1} {2}"

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
  • string1
  • string2
  • string3
  • " "
  • " "
  • string1 + " "
  • string1 + " " + string2
  • string1 + " " + string2 + " "
  • string1 + " " + string2 + " " + string 3


result = string.Format("{0} {1} {2}", string1, string2, string3)

This creates 5 references

  • result
  • string1
  • string2
  • string3
  • "{0} {1} {2}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文