C#中字符串前面的@是什么?
这是 C#(或者可能是 VB.net)的 .NET 问题,但我试图弄清楚以下声明之间有什么区别:
string hello = "hello";
与
string hello_alias = @"hello";
在控制台上打印没有区别,长度属性是相同的。
This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations:
string hello = "hello";
vs.
string hello_alias = @"hello";
Printing out on the console makes no difference, the length properties are the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
它将字符串标记为逐字字符串文字 - 字符串中通常会被解释的任何内容因为转义序列被忽略。
因此
"C:\\Users\\Rich"
与@"C:\Users\Rich"
相同,但有一个例外:需要转义序列双引号。 要转义双引号,您需要将两个双引号放在一行中。 例如,
@""""
的计算结果为"
。It marks the string as a verbatim string literal - anything in the string that would normally be interpreted as an escape sequence is ignored.
So
"C:\\Users\\Rich"
is the same as@"C:\Users\Rich"
There is one exception: an escape sequence is needed for the double quote. To escape a double quote, you need to put two double quotes in a row. For instance,
@""""
evaluates to"
.这是一个逐字字符串。 这意味着不应用转义。 例如:
这里的
verbatim
和regular
有相同的内容。它还允许多行内容 - 这对于 SQL 来说非常方便:
逐字字符串文字所需的一点转义是通过将其加倍来获得双引号 ("):
It's a verbatim string literal. It means that escaping isn't applied. For instance:
Here
verbatim
andregular
have the same contents.It also allows multi-line contents - which can be very handy for SQL:
The one bit of escaping which is necessary for verbatim string literals is to get a double quote (") which you do by doubling it:
“@”还有另一个含义:将其放在变量声明前面允许您使用保留关键字作为变量名。
例如:
我只发现了一两个合法用途。 主要在 ASP.NET MVC 中,当您想做这样的事情时:
这会产生一个 HTML 链接,例如:
否则,您将不得不使用“Class”,它不是保留关键字,但大写的“C”不遵循 HTML标准,但看起来不正确。
An '@' has another meaning as well: putting it in front of a variable declaration allows you to use reserved keywords as variable names.
For example:
I've only found one or two legitimate uses for this. Mainly in ASP.NET MVC when you want to do something like this:
Which would produce an HTML link like:
Otherwise you would have to use 'Class', which isn't a reserved keyword but the uppercase 'C' does not follow HTML standards and just doesn't look right.
既然您也明确要求使用 VB,那么让我补充一下,这种逐字字符串语法在 VB 中不存在,仅在 C# 中存在。 相反,所有字符串在 VB 中都是逐字的(除了它们不能包含换行符这一事实,这与 C# 逐字字符串不同):
VB 中不存在转义序列(除了双引号字符之外 ) ,就像在 C# 逐字字符串中一样),这使得一些事情变得更加复杂。 例如,要在 VB 中编写以下代码,您需要使用串联(或任何其他方式来构造字符串)
在 VB 中,这将编写如下:(
&
是 VB 字符串可以同样使用连接运算符。)Since you explicitly asked for VB as well, let me just add that this verbatim string syntax doesn't exist in VB, only in C#. Rather, all strings are verbatim in VB (except for the fact that they cannot contain line breaks, unlike C# verbatim strings):
Escape sequences don't exist in VB (except for the doubling of the quote character, like in C# verbatim strings) which makes a few things more complicated. For example, to write the following code in VB you need to use concatenation (or any of the other ways to construct a string)
In VB this would be written as follows:
(
&
is the VB string concatenation operator.+
could equally be used.)http://msdn.microsoft.com/en-us/library/aa691090.aspx
C# 支持两种形式的字符串文字:常规字符串文字和逐字字符串文字。
常规字符串文字由零个或多个用双引号括起来的字符组成,如“hello”,并且可能包括简单的转义序列(例如制表符的 \t)以及十六进制和 Unicode 转义序列。
逐字字符串文字由 @ 字符后跟双引号字符、零个或多个字符以及结束双引号字符组成。 一个简单的例子是@“hello”。 在逐字字符串文字中,分隔符之间的字符将逐字解释,唯一的例外是引号转义序列。 特别是,简单转义序列以及十六进制和 Unicode 转义序列不会按逐字字符串文字进行处理。 逐字字符串文字可以跨越多行。
http://msdn.microsoft.com/en-us/library/aa691090.aspx
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. A verbatim string literal may span multiple lines.
这是一个逐字字符串,并更改转义规则 - 现在转义的唯一字符是“,转义为””。这对于文件路径和正则表达式特别有用:
等
This is a verbatim string, and changes the escaping rules - the only character that is now escaped is ", escaped to "". This is especially useful for file paths and regex:
etc
复制自 MSDN:
Copied from MSDN:
将
@
放在字符串前面使您可以使用特殊字符,例如反斜杠或双引号,而不必使用特殊代码或转义字符。所以你可以写:
而不是:
Putting a
@
in front of a string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters.So you can write:
instead of:
解释很简单。 为了表示字符串
"string\"
,编译器需要"string\\"
,因为\
是转义字符。 如果您使用@"string\"
代替,您可以忘记\\
。The explanation is simple. To represent the string
"string\"
, the compiler needs"string\\"
because\
is an escape character. If you use@"string\"
instead, you can forget about\\
.