“@”在 C# 中是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2361857/
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 does "@" mean in C#
提问by Mike
Possible Duplicate:
when to use @ in c# ?
可能重复:
何时在 c# 中使用 @?
F.e. string sqlSelect = @"SELECT * FROM Sales".
铁 string sqlSelect = @"SELECT * FROM Sales".
采纳答案by Sarfraz
It means interpret the following string as literal. Meaning, the \
in the string will actually be a "\"
in the output, rather than having to put "\\"
to mean the literal character
这意味着将以下字符串解释为文字。意思是,\
字符串中的实际上将是"\"
输出中的 a,而不必"\\"
表示文字字符
回答by Dested
It allows you to have a string with a \
delimiter in it. @"C:\A\b\c\d\e\f"
is legal.
它允许您拥有一个带有\
分隔符的字符串。@"C:\A\b\c\d\e\f"
是合法的。
回答by logicnp
It means there is no need to escape characters in such a string.
这意味着不需要在这样的字符串中转义字符。
So if you want to write the path for c:\Windows, you can write it as
所以如果你想写c:\Windows的路径,你可以写成
string path = "c:\Windows"; // Note escaped '\'
OR
或者
string path = @"c:\Windows"; // '\' need not be escaped
回答by Kangkan
Used for string literal. It marks the string within the quote (") marks as the value without applying any interpretation for symbols in that string.
用于字符串文字。它将引号 (") 内的字符串标记为值,而不对该字符串中的符号应用任何解释。
回答by cyberzed
Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:
逐字字符串文字以@ 开头,也用双引号括起来。例如:
@"good morning" // a string literal
Nicked from, have a look at the last few lines above the example for more information. http://msdn.microsoft.com/en-us/library/362314fe.aspx
取自,查看示例上方的最后几行以获取更多信息。 http://msdn.microsoft.com/en-us/library/362314fe.aspx
回答by Cameron MacFarland
There are two types of string literals, regular and verbatim. The @ symbol makes it a verbatim string literal.
有两种类型的字符串文字,常规和逐字。@ 符号使其成为逐字字符串文字。
回答by Sergej Andrejev
Before string it allows different string formating rules. You can't use backslash to specify special symbols and "" (double quotes become quotes). I find this format very useful for regular expressions
在字符串之前,它允许不同的字符串格式规则。您不能使用反斜杠来指定特殊符号和“”(双引号变成引号)。我发现这种格式对于正则表达式非常有用
Example
例子
Console.WriteLine(@"\n""\/a"); // outputs \n"\/a
Console.WriteLine("\n\"\"\/a"); // outputs \n"\/a
You might also seen @ symbol before variable. In such case it allows using special C# keywords as variables.
您可能还会在变量之前看到 @ 符号。在这种情况下,它允许使用特殊的 C# 关键字作为变量。
Example:
例子:
var @switch = 1;
var @if = "test";
回答by Salem309
In C and C++, string has some special characters called "escape characters". For example \
, &
and the "
itself is an escape character!
在 C 和 C++ 中,字符串有一些称为“转义字符”的特殊字符。例如\
,&
而"
本身就是一个转义字符!
In the very normal way, you to print a statement like:
以非常正常的方式,您可以打印如下语句:
Nancy Said Hello World! & smiled
南希说,世界你好!&微笑
you had to set your string like next
你必须像 next 一样设置你的字符串
string str = "Nancy said Hello World! \& smiled.";
But people in Microsoft made a new cool feature in C# compiler so you can escape the headache of handling the escape characters by adding @
before any string, and the compiler will handle all the escape characters for you by himself. For the last example you can have this in C# like next:
但是微软的人在 C# 编译器中做了一个很酷的新特性,这样你就可以避免@
在任何字符串前添加处理转义字符的头痛,编译器会自己为你处理所有的转义字符。对于最后一个示例,您可以在 C# 中使用它,如下所示:
string str = @"Nancy said Hello World! & smiled.";