@ 前缀对 C# 中的字符串文字有何作用
我阅读了一些 C# 文章,使用 Path.Combine(part1,part2) 组合路径。
它使用以下内容:
string part1 = @"c:\temp";
string part2 = @"assembly.txt";
请问part1和part2中的@
有什么用?
I read some C# article to combine a path using Path.Combine
(part1,part2).
It uses the following:
string part1 = @"c:\temp";
string part2 = @"assembly.txt";
May I know what is the use of @
in part1 and part2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
@
与任何方法都不相关。这意味着您不需要转义符号后面的字符串中的特殊字符:
等于
这样的字符串称为“逐字”或@引用。请参阅 MSDN。
@
is not related to any method.It means that you don't need to escape special characters in the string following to the symbol:
is equal to
Such string is called 'verbatim' or @-quoted. See MSDN.
正如其他人所说,这是一种方法,这样您就不需要转义特殊字符,并且在指定文件路径时非常有用。
另一种用法是当您有大字符串并希望它跨多行而不是长行显示时。
As other have said its one way so that you don't need to escape special characters and very useful in specifying file paths.
One more usage is when you have large strings and want it to be displayed across multiple lines rather than a long one.
如 C# 语言规范 4.0 中所述:
As stated in C# Language Specification 4.0:
它表示逐字字符串文字,并允许您使用通常具有特殊含义的某些字符,例如
\
(通常是转义字符)和换行符。因此,它在处理 Windows 路径时非常有用。如果不使用
@
,示例的第一行必须是:更多信息 这里。
It denotes a verbatim string literal, and allows you to use certain characters that normally have special meaning, for example
\
, which is normally an escape character, and new lines. For this reason it's very useful when dealing with Windows paths.Without using
@
, the first line of your example would have to be:More information here.
@ 只是表示指定字符串的不同方式,这样您就不必使用 转义字符。唯一需要注意的是双引号需要是“”来代表单个“。
The @ just indicates a different way of specifying a string such that you do not have to escape characters with . the only caveat is that double quotes need to be "" to represent a single ".
使用@,您不必转义特殊字符。
因此,您必须编写不带@的“c:\\ temp”,
如果更准确地说,它被称为“逐字”字符串。您可以在这里阅读相关内容:
http://msdn.microsoft.com/en-我们/library/aa691090(v=vs.71).aspx
With @ you dont have to escape special characters.
So you would have to write "c:\\temp" without @
If more presise it is called 'verbatim' strings. You could read here about it:
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx