@ 前缀对 C# 中的字符串文字有何作用

发布于 2024-11-09 22:52:47 字数 199 浏览 0 评论 0原文

我阅读了一些 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 技术交流群。

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

发布评论

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

评论(6

初心未许 2024-11-16 22:52:47

@ 与任何方法都不相关。

这意味着您不需要转义符号后面的字符串中的特殊字符:

@"c:\temp"

等于

"c:\\temp"

这样的字符串称为“逐字”或@引用。请参阅 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:

@"c:\temp"

is equal to

"c:\\temp"

Such string is called 'verbatim' or @-quoted. See MSDN.

清风夜微凉 2024-11-16 22:52:47

正如其他人所说,这是一种方法,这样您就不需要转义特殊字符,并且在指定文件路径时非常有用。

string s1 =@"C:\MyFolder\Blue.jpg";

另一种用法是当您有大字符串并希望它跨多行而不是长行显示时。

string s2 =@"This could be very large string something like a Select query
which you would want to be shown spanning across multiple lines 
rather than scrolling to the right and see what it all reads up";

As other have said its one way so that you don't need to escape special characters and very useful in specifying file paths.

string s1 =@"C:\MyFolder\Blue.jpg";

One more usage is when you have large strings and want it to be displayed across multiple lines rather than a long one.

string s2 =@"This could be very large string something like a Select query
which you would want to be shown spanning across multiple lines 
rather than scrolling to the right and see what it all reads up";
没企图 2024-11-16 22:52:47

C# 语言规范 4.0 中所述:

2.4.4.5 字符串文字

C#支持两种形式的字符串
文字:常规字符串文字和
逐字字符串文字。一个常规的
字符串文字由零或
更多字符用双引号括起来
引号,如“你好”,并且可能包括
两个简单的转义序列(例如
\t 表示制表符),以及
十六进制和 Unicode 转义
序列。逐字字符串文字
由 @ 字符后跟组成
双引号字符,零个或多个
字符和结束双引号
特点。一个简单的例子是
@“你好”。在逐字字符串中
文字,之间的字符
分隔符逐字解释,
唯一的例外是
引用转义序列。尤其,
简单的转义序列,以及
十六进制和 Unicode 转义
序列不被处理
逐字字符串文字。

As stated in C# Language Specification 4.0:

2.4.4.5 String literals

C# supports two forms of string
literals: regular string literals and
verbatim string literals. A regular
string literal consists of zero or
more characters enclosed in double
quotes, as in "hello", and may include
both simple escape sequences (such as
\t for the tab character), and
hexadecimal and Unicode escape
sequences. A verbatim string literal
consists of an @ character followed by
a double-quote character, zero or more
characters, and a closing double-quote
character. A simple example is
@"hello". In a verbatim string
literal, the characters between the
delimiters are interpreted verbatim,
the only exception being a
quote-escape-sequence. In particular,
simple escape sequences, and
hexadecimal and Unicode escape
sequences are not processed in
verbatim string literals.

初见 2024-11-16 22:52:47

它表示逐字字符串文字,并允许您使用通常具有特殊含义的某些字符,例如 \(通常是转义字符)和换行符。因此,它在处理 Windows 路径时非常有用。

如果不使用 @,示例的第一行必须是:

string part1 = "c:\\temp";

更多信息 这里

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:

string part1 = "c:\\temp";

More information here.

孤芳又自赏 2024-11-16 22:52:47

@ 只是表示指定字符串的不同方式,这样您就不必使用 转义字符。唯一需要注意的是双引号需要是“”来代表单个“。

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 ".

海之角 2024-11-16 22:52:47

使用@,您不必转义特殊字符。

因此,您必须编写不带@的“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

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