什么时候在c#中使用@?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1057926/
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
when to use @ in c#?
提问by Moon
I use @ symbol with local path only, but when do I use @ exactly?
我只在本地路径中使用 @ 符号,但是我什么时候使用 @ 呢?
采纳答案by ChrisF
You use @ before strings to avoid having to escape special characters.
您在字符串前使用 @ 以避免转义特殊字符。
This from the MSDN:
这来自MSDN:
The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"
@-quoting 的好处是不处理转义序列,这样写起来很方便,例如全限定文件名:
@"c:\Docs\Source\a.txt" // 而不是 "c:\\Docs\\Source\\a.txt"
回答by Aamir
AFAIK, You can use @ at any place where you don't want to let the default meaning of the thing persist. For example, @class will make class an identifier. @bool will make bool an identifier instead of a keyword.
AFAIK,您可以在任何不想让事物的默认含义持续存在的地方使用 @。例如,@class 将使类成为标识符。@bool 将使 bool 成为标识符而不是关键字。
You know the usage of @ before strings. It is used to indicate that take all the text of the string literally and don't treat any character in the string specially.
你知道@在字符串之前的用法。用于表示从字面上取字符串的所有文本,不对字符串中的任何字符进行特殊处理。
Edit: An yes, another thing to this is that @Keyword
is compiled into a Keyword
in IL.
编辑:是的,另一件事是在 IL 中@Keyword
编译成 a Keyword
。
See this Linkfor more details.
回答by Rob
Verbatim Strings
逐字字符串
An @
before a string literal in C# denotes a verbatim string. In a verbatim string, only the quote escape sequence (""
) is parsed as an escape sequence; all others, e.g. \n
, \t
etc. are ignored.
一个@
在C#中的字符串字面之前表示逐字字符串。在逐字字符串中,只有引号转义序列 ( ""
) 被解析为转义序列;所有其他的,例如\n
,\t
等等都被忽略了。
You'll have seen this syntax used with file paths, etc. because it's convenient to have the compiler ignore the backslashes in the path, rather than having to double-escape them, e.g.
您会看到这种语法与文件路径等一起使用,因为让编译器忽略路径中的反斜杠很方便,而不必对它们进行双重转义,例如
var s = @"c:\Some\File\Path.txt";
is a little easier to read than
比阅读更容易
var s = "c:\Some\File\Path.txt";
Reserved Words
保留字
You can also prefix identifierswith @
in order to allow using reserved words in identifiers. For example, @class
can be used as an identifier, whereas class
wouldn't be permitted. In this specific case, @class
is a little less jarring (at least, I find) than the usual convention of klass
or clazz
which are often used to work around this restriction in other languages.
您还可以为标识符添加前缀@
以允许在标识符中使用保留字。例如,@class
可以用作标识符,而class
不允许使用。在这种特定情况下,与其他语言中通常用于解决此限制@class
的klass
或的通常约定相比,不那么刺耳(至少,我发现)clazz
。
回答by Chris Morley
You can prefix a string with the @ sign to avoid having to type 2 backslashes to mean one backslash. This is why it is often used for local path because it saves some typing, and simplifies what you are looking at when you read it later. When you have a bunch of double quotes and other escape characters, aka special characters - that's when you want the @ sign. When you use the @ sign, make sure to put just one backslash when you mean backslash. When using the @ you want to use the double quote character, put two double quotes instead of backslash, double quote.
您可以使用 @ 符号作为字符串的前缀,以避免必须键入 2 个反斜杠来表示一个反斜杠。这就是为什么它经常用于本地路径的原因,因为它节省了一些输入,并简化了您以后阅读时所看到的内容。当你有一堆双引号和其他转义字符,也就是特殊字符时 - 这就是你想要@ 符号的时候。当您使用 @ 符号时,请确保在表示反斜杠时只输入一个反斜杠。使用@时要使用双引号字符,放两个双引号而不是反斜杠,双引号。
String path = "C:\path\to\my\file";
vs
对比
String path = @"C:\path\to\my\file"; //@ says one backslash is enough
here is another example:
这是另一个例子:
String quotation = "He said, \"Wow!\""; //backslashes say not to end string
vs.
对比
String quotation = @"He said, ""Wow!"""; //need a different method of denoting double quote
回答by Sauron
If you want to use keywords as variable names
如果要使用关键字作为变量名
string @string = "Hi";