[英]Verbatim string literals v escape sequences
Is there any difference in how the C# compiler or .NET run-time handles verbatim string literals versus using escape sequences (ie performance) or is it just a matter of design time style? C#编译器或.NET运行时如何处理逐字字符串文字与使用转义序列(即性能)之间是否有任何区别?还是仅与设计时间风格有关? EG: 例如:
var pathA = "c:\\somewhere";
var pathB = @"c:\somewhere";
I would imagine they are compiled the same and it doesn't matter, but was just curious. 我可以想象它们是一样编译的,没关系,但是只是很好奇。
Any difference here is limited strictly to the compiler; 这里的任何区别都严格限于编译器; the IL and runtime have no concept of verbatim vs escaped - it just has the string . IL和运行时没有逐字对比转义的概念-它只有字符串 。
As for which to choose: whichever is more convenient ;p I almost always use verbatim string literals if there are unusual characters, as that allows for multi-line strings very easily and visually. 至于选择哪一个:哪个更方便; p如果有不寻常的字符,我几乎总是使用逐字字符串,因为这样可以很容易地在视觉上实现多行字符串。
As an interesting case: 作为一个有趣的例子:
bool areSame = ReferenceEquals("c:\\somewhere", @"c:\somewhere"); // true
which tells are they are exactly the same string instance (thanks to "interning"). 这表明它们是完全相同的字符串实例(由于“ interning”)。 They aren't just equivalent ; 它们不仅是等价的 ; they are the same string instance to the runtime. 它们是运行时的相同字符串实例 。 It is therefore impossible that they can be (to the runtime) different in any way. 因此,它们不可能 (在运行时)以任何方式有所不同。
They are exactly the same. 他们是完全一样的。 Try to decompile the two versions with a decompiler. 尝试使用反编译器反编译两个版本。
It's only a matter of convenience for developers when writing it in the code. 对于开发人员而言,在代码中编写代码只是一个方便的问题。
The @ sign in front of a string tells the compiler to ignore any embeded escape sequences. 字符串前面的@符号告诉编译器忽略任何嵌入的转义序列。
string "\\"" would yield a single double quote. string "\\" would yield a single back slash string @"\\" would yield two backslashes 字符串“ \\”“将产生一个双引号。字符串” \\“将产生一个反斜杠字符串@” \\“将产生两个反斜杠
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.