逐字字符串v转义序列 [英] Verbatim string literals v escape sequences
问题描述
有没有任何区别如何C#编译器或.NET运行时处理逐字字符串与使用转义序列(即性能)还是只是设计时间样式的事?例如:
Is there any difference in how the C# compiler or .NET run-time handles verbatim string literals versus using escape sequences (i.e. performance) or is it just a matter of design time style? E.G.:
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.
推荐答案
在这里的任何差异被严格限制编译器;白细胞介素和运行时有逐字VS没有概念逃脱 - 它只是具有的字符串的
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.
至于可供选择:取其更方便,P我几乎总是使用逐字字符串,如果有特殊字符,作为能够多行字符串很容易和视觉
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.
作为一个有趣的例子:
bool areSame = ReferenceEquals("c:\\somewhere", @"c:\somewhere"); // true
它告诉是他们的究竟的相同的字符串实例(感谢实习)。他们不只是相当于的;他们的相同的字符串实例应用于运行。因此,它是<青霉>不可能的,它们可以是(到运行时)以任何方式不同。
which tells are they are exactly the same string instance (thanks to "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.
这篇关于逐字字符串v转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!