反斜杠在我的网络服务中无法正常工作

发布于 2024-11-18 02:19:18 字数 252 浏览 4 评论 0原文

我在 Web 服务中有一行简单的代码:

instance = @"\instanceNameHere";

但输出始终相同。

\\instanceNameHere

如果我删除@并使用两个斜杠,我会得到相同的结果。我以前从未见过这种情况,我的 Google fu 也让我失望了。我什至写了一个简单的应用程序,结果是正确的。那么为什么它会在网络服务中起作用呢?

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 Google-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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤寂小茶 2024-11-25 02:19:18

它在调试器中为您转义斜杠,因此您知道它是斜杠,而不是像 \t 这样的转义序列。如果调试器没有这样做,那么如何区分该字符串

\t

与调试器中的字符串

<tab>

,因为后者在转义序列中由 \t 表示?因此,前者显示为

\\t

,后者显示为

\t

将其写入流或控制台,您将看到它只有一个斜杠,或者执行 instance.Length 并与字符数进行比较。您将在控制台上看到 17,而 \\instanceNameHere 有 18 个字符。

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.

雅心素梦 2024-11-25 02:19:18

调试器将字符串显示为 C# 文字。所以它用转义字符来显示它们。它还会将回车符显示为 \r,将制表符显示为 \t。这纯粹是为了可视化——字符串实际上并不包含这些转义字符。如果将其写到日志中,它将不包含转义字符——它看起来会如您所期望的那样。

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.

请持续率性 2024-11-25 02:19:18

任何格式的 UNC 名称,始终以两个反斜杠字符 (“\”) 开头。

链接

更新:请参阅@Jason上面发帖!我没有意识到他正在检查调试器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文