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
.
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).
Debugger visualizers display strings in the form in which they would appear in C# code. Since
\
is used to escape characters in non-verbatum C# strings,\\
is the correct escaped form.The
\\
is used because the\
is an escape character and is need to represent the a single\
.So it is saying treat the first
\
as an escape character and then the second\
is taken as the actual value. If not the next character after the first\
would be parsed as an escaped character.Here is a list of available escape characters:
EDIT: To answer your question regarding
Split
, it should be no issue. UseSplit
as you would normally. The\\
will be treated as only the one character of\
.Okay, so the answers above are not wholly correct. As such I am adding my findings for the next person who reads this post.
You cannot split a string using any of the chars in the table above if you are reading said string(s) from an external source.
i.e,
will not split by those chars. However internally created strings work fine.
i.e.,
This may not hold true for other methods of reading text from a file. I am unsure as I have not tested with other methods. With that in mind, it is probably best not to use those chars for delimiting strings!
.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
@
sign and removes the need o escape\\
characters\\
characters need to escape themselvesThe 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.