C#中字符串前面的@是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/556133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the @ in front of a string in C#?
提问by Klaw
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:
这是 C#(或可能是 VB.net)的 .NET 问题,但我试图找出以下声明之间的区别:
string hello = "hello";
vs.
对比
string hello_alias = @"hello";
Printing out on the console makes no difference, the length properties are the same.
在控制台上打印没有区别,长度属性是相同的。
采纳答案by Richard Ev
It marks the string as a verbatim string literal- anything in the string that would normally be interpreted as an escape sequenceis ignored.
它将字符串标记为逐字字符串文字- 字符串中通常被解释为转义序列的任何内容都将被忽略。
So "C:\\Users\\Rich"
is the same as @"C:\Users\Rich"
所以"C:\\Users\\Rich"
是一样的@"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 "
.
有一个例外:双引号需要转义序列。要转义双引号,您需要连续放置两个双引号。例如,@""""
评估为"
。
回答by Jon Skeet
It's a verbatim string literal. It means that escaping isn't applied. For instance:
这是一个逐字字符串文字。这意味着不应用转义。例如:
string verbatim = @"foo\bar";
string regular = "foo\bar";
Here verbatim
and regular
have the same contents.
这里verbatim
和regular
有相同的内容。
It also allows multi-line contents - which can be very handy for SQL:
它还允许多行内容——这对于 SQL 来说非常方便:
string select = @"
SELECT Foo
FROM Bar
WHERE Name='Baz'";
The one bit of escaping which is necessary for verbatim string literals is to get a double quote (") which you do by doubling it:
逐字字符串文字所必需的一点转义是得到一个双引号 ("),您可以将它加倍:
string verbatim = @"He said, ""Would you like some coffee?"" and left.";
string regular = "He said, \"Would you like some coffee?\" and left.";
回答by Ed Guiness
http://msdn.microsoft.com/en-us/library/aa691090.aspx
http://msdn.microsoft.com/en-us/library/aa691090.aspx
C# supports two forms of string literals: regular string literals and verbatim string literals.
C# 支持两种形式的字符串文字:常规字符串文字和逐字字符串文字。
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.
常规字符串文字由用双引号括起来的零个或多个字符组成,如“hello”,并且可能包含简单的转义序列(例如制表符 \t)以及十六进制和 Unicode 转义序列。
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.
逐字字符串文字由@ 字符后跟一个双引号字符、零个或多个字符和一个结束双引号字符组成。一个简单的例子是@"hello"。在逐字字符串文字中,分隔符之间的字符被逐字解释,唯一的例外是引号转义序列。特别是,不会在逐字字符串文字中处理简单的转义序列以及十六进制和 Unicode 转义序列。逐字字符串文字可能跨越多行。
回答by Marc Gravell
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:
这是一个逐字字符串,并更改了转义规则 - 现在转义的唯一字符是“,转义为“”。这对于文件路径和正则表达式特别有用:
var path = @"c:\some\location";
var tsql = @"SELECT *
FROM FOO
WHERE Bar = 1";
var escaped = @"a "" b";
etc
等等
回答by Presidenten
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.
将 a@
放在字符串前面使您可以使用特殊字符,例如反斜杠或双引号,而无需使用特殊代码或转义字符。
So you can write:
所以你可以写:
string path = @"C:\My path\";
instead of:
代替:
string path = "C:\My path\";
回答by aanund
Copied from MSDN:
从MSDN复制:
At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string
@"C:\files.txt"
will appear in the watch window as"C:\\files.txt"
.
在编译时,逐字字符串被转换为具有所有相同转义序列的普通字符串。因此,如果您在调试器监视窗口中查看逐字字符串,您将看到编译器添加的转义字符,而不是源代码中的逐字版本。例如,逐字字符串
@"C:\files.txt"
将在监视窗口中显示为"C:\\files.txt"
。
回答by Konrad Rudolph
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, allstrings are verbatim in VB (except for the fact that they cannot contain line breaks, unlike C# verbatim strings):
由于您还明确要求使用 VB,让我补充一点,VB 中不存在这种逐字字符串语法,仅在 C# 中存在。相反,所有字符串在 VB 中都是逐字的(除了它们不能包含换行符,与 C# 逐字字符串不同):
Dim path = "C:\My\Path"
Dim message = "She said, ""Hello, beautiful world."""
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)
VB 中不存在转义序列(除了引号字符的加倍,如在 C# 逐字字符串中),这使一些事情变得更加复杂。例如,要在 VB 中编写以下代码,您需要使用连接(或任何其他方式来构造字符串)
string x = "Foo\nbar";
In VB this would be written as follows:
在VB中,这将被写成如下:
Dim x = "Foo" & Environment.NewLine & "bar"
(&
is the VB string concatenation operator. +
could equally be used.)
(&
是 VB 字符串连接运算符。+
同样可以使用。)
回答by JulianR
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:
例如:
string @class = "something";
int @object = 1;
I've only found one or two legitimate uses for this. Mainly in ASP.NET MVC when you want to do something like this:
我只找到了一两个合法的用途。主要是在 ASP.NET MVC 中,当你想做这样的事情时:
<%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>
Which would produce an HTML link like:
这将产生一个 HTML 链接,如:
<a href="/Controller/Action" class="some_css_class">Text</a>
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.
否则,您将不得不使用“Class”,它不是保留关键字,但大写的“C”不符合 HTML 标准并且看起来不正确。
回答by Ray Wu
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 \\
.
解释很简单。为了表示字符串"string\"
,编译器需要"string\\"
因为\
是一个转义字符。如果你@"string\"
改用,你可以忘记\\
.