C#中字符串前面的@是什么?

发布于 2024-07-14 11:48:14 字数 202 浏览 9 评论 0原文

这是 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 技术交流群。

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

发布评论

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

评论(9

迷荒 2024-07-21 11:48:14

它将字符串标记为逐字字符串文字 - 字符串中通常会被解释的任何内容因为转义序列被忽略。

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

花开浅夏 2024-07-21 11:48:14

这是一个逐字字符串。 这意味着不应用转义。 例如:

string verbatim = @"foo\bar";
string regular = "foo\\bar";

这里的verbatimregular有相同的内容。

它还允许多行内容 - 这对于 SQL 来说非常方便:

string select = @"
SELECT Foo
FROM Bar
WHERE Name='Baz'";

逐字字符串文字所需的一点转义是通过将其加倍来获得双引号 ("):

string verbatim = @"He said, ""Would you like some coffee?"" and left.";
string regular = "He said, \"Would you like some coffee?\" and left.";

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.

It also allows multi-line contents - which can be very handy for 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.";
分開簡單 2024-07-21 11:48:14

“@”还有另一个含义:将其放在变量声明前面允许您使用保留关键字作为变量名。

例如:

string @class = "something";
int @object = 1;

我只发现了一两个合法用途。 主要在 ASP.NET MVC 中,当您想做这样的事情时:

<%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>

这会产生一个 HTML 链接,例如:

<a href="/Controller/Action" class="some_css_class">Text</a>

否则,您将不得不使用“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:

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:

<%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>

Which would produce an HTML link like:

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

站稳脚跟 2024-07-21 11:48:14

既然您也明确要求使用 VB,那么让我补充一下,这种逐字字符串语法在 VB 中不存在,仅在 C# 中存在。 相反,所有字符串在 VB 中都是逐字的(除了它们不能包含换行符这一事实,这与 C# 逐字字符串不同):

Dim path = "C:\My\Path"
Dim message = "She said, ""Hello, beautiful world."""

VB 中不存在转义序列(除了双引号字符之外 ) ,就像在 C# 逐字字符串中一样),这使得一些事情变得更加复杂。 例如,要在 VB 中编写以下代码,您需要使用串联(或任何其他方式来构造字符串)

string x = "Foo\nbar";

在 VB 中,这将编写如下:(

Dim x = "Foo" & Environment.NewLine & "bar"

& 是 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):

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)

string x = "Foo\nbar";

In VB this would be written as follows:

Dim x = "Foo" & Environment.NewLine & "bar"

(& 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.

┈┾☆殇 2024-07-21 11:48:14

这是一个逐字字符串,并更改转义规则 - 现在转义的唯一字符是“,转义为””。这对于文件路径和正则表达式特别有用:

var path = @"c:\some\location";
var tsql = @"SELECT *
            FROM FOO
            WHERE Bar = 1";
var escaped = @"a "" b";

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

丘比特射中我 2024-07-21 11:48:14

复制自 MSDN

在编译时,逐字字符串将转换为具有所有相同转义序列的普通字符串。 因此,如果您在调试器监视窗口中查看逐字字符串,您将看到编译器添加的转义字符,而不是源代码中的逐字版本。 例如,逐字字符串 @"C:\files.txt" 将在监视窗口中显示为 "C:\\files.txt"

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

蓝天白云 2024-07-21 11:48:14

@ 放在字符串前面使您可以使用特殊字符,例如反斜杠或双引号,而不必使用特殊代码或转义字符。

所以你可以写:

string path = @"C:\My path\";

而不是:

string path = "C:\\My path\\";

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:

string path = @"C:\My path\";

instead of:

string path = "C:\\My path\\";
夏末染殇 2024-07-21 11:48:14

解释很简单。 为了表示字符串 "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 \\.

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