本文介绍了打破C#源代码长串的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是最佳实践打破C#源代码的长字符串。这是字符串

I am wondering what is the "best practice" to break long strings in C# source code. Is this string

"string1"+
"string2"+
"string3"

在编译过程中或在运行时连接起来?

concatenated during compiling or in run time?

推荐答案

这是在编译时完成。 ,这是完全等同于string1string2string3

It's done at compile time. That's exactly equivalent to "string1string2string3".

假设你有:

string x = "string1string2string3"
string y = "string1" + "string2" + "string3"


$ b $ :b

编译器将实习使得x和y引用同一个对象上执行相应的

The compiler will perform appropriate interning such that x and y refer to the same objects.

编辑:有一个关于很多讨论StringBuilder的的答案和评论。许多开发人员似乎认为,字符串连接应的总是的用的StringBuilder 完成。这是一个overgeneralisation - 这是值得理解的。

There's a lot of talk about StringBuilder in the answers and comments. Many developers seem to believe that string concatenation should always be done with StringBuilder. That's an overgeneralisation - it's worth understanding why StringBuilder is good in some situations, and not in others.

这篇关于打破C#源代码长串的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:49