为什么.NET额外的斜杠添加到路径已经存在的斜线? [英] Why does .NET add an additional slash to the already existent slashes in a path?
问题描述
我注意到,C#增加了额外的斜线( \\
)为路径。考虑路径 C:\\测试
。当我检查字符串文本Visualiser的这条道路,实际的字符串是 C:\\\\测试
。
I've noticed that C# adds additional slashes (\
) to paths. Consider the path C:\Test
. When I inspect the string with this path in the text visualiser, the actual string is C:\\Test
.
这是为什么?它混淆了我,因为有时我可能要拆分的路径了(使用 string.Split()
),但不得不怀疑要使用的字符串(一个或两个斜线)
Why is this? It confuses me, as sometimes I may want to split the path up (using string.Split()
), but have to wonder which string to use (one or two slashes).
推荐答案
.NET是不添加任何东西到你这里的字符串。你的看到的是的调试器如何选择显示的字符串的效果。 C#字符串可以重新在2形式psented $ P $
.Net is not adding anything to your string here. What your seeing is an effect of how the debugger chooses to display strings. C# strings can be represented in 2 forms
- 逐字字符串:使用
@
标志pfixed $ P $而且无需Ò逃逸\\\\
字符 - 普通字符串:标准C风格的字符串,其中
\\\\
字符需要逃避自己
- Verbatim Strings: Prefixed with an
@
sign and removes the need o escape\\
characters - Normal Strings: Standard C style strings where
\\
characters need to escape themselves
调试器将显示一个字符串作为一个普通的字符串与逐字字符串。这只是显示的问题,不过,这并不影响它的潜在价值。
The debugger will display a string literal as a normal string vs. a verbatim string. It's just an issue of display though, it doesn't affect it's underlying value.
这篇关于为什么.NET额外的斜杠添加到路径已经存在的斜线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!