Backslashes not working properly in my web service
I have a simple line of code in a web service:
instance = @"\instanceNameHere";
Yet the output is always the same.
\\instanceNameHere
If I remove the @ and use two slashes, I get the same result. I've never seen this before and my G开发者_StackOverflow中文版oogle-fu has failed me. I even wrote a simple app and the result was correct. So why is it acting up in the web service?
It's escaping the slash for you in the debugger so you know that it's a slash and not an escape sequence like \t
. If the debugger did not do this, how could you distinguish the string
\t
from the string
<tab>
in the debugger since the latter is represented in an escape sequence by \t
? Therefor the former is shown as
\\t
and the latter as
\t
Write it to a stream or the console and you'll see that it only has one slash, or do instance.Length
and compare to a count of the characters. You'll see 17
on the console, whereas \\instanceNameHere
has eighteen characters.
The debugger displays strings as C# literals. So it's displaying them with characters escaped. It would also show carriage returns as \r
and tabs as \t
. This is purely for visualization -- the string does not literally contain these escape characters. If you write it out to a log, it will not include the escape characters -- it will look as you expect.
A UNC name of any format, which always start with two backslash characters ("\").
Link
Update : Please see @Jason post above! I didn't realise he was checking in the debugger.
精彩评论