在 C# 变量名前放置 @ 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/254669/
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 placing a @ in front of a C# variable name do?
提问by Orion Adrian
I've been working with some C# legacy code and I've been seeing a lot of @ symbols in front of variable names. What does this signify or do?
我一直在使用一些 C# 遗留代码,并且在变量名前面看到了很多 @ 符号。这意味着什么或做什么?
Currently I'm seeing it a lot in front of variables with common names that aren't reserved. E.g.:
目前,我在具有未保留的通用名称的变量前面看到了很多。例如:
MyProcedure(@step.LoadInstanceId, @step.ResultCode, @step.StatusCode);
Given that step isn't a reserved word, is there any reason that they should be escaped?
鉴于该步骤不是保留字,是否有任何理由应该对其进行转义?
采纳答案by ripper234
It's just a way to allow declaring reserved keywords as vars.
这只是一种允许将保留关键字声明为 vars 的方法。
void Foo(int @string)
回答by Jacob Carpenter
This escapes reserved words in C#.
这会转义 C# 中的保留字。
回答by Jacob Carpenter
It allows you to use a reserved word, like 'public' for example, as a variable name.
它允许您使用保留字(例如“public”)作为变量名。
string @public = "foo";
I would not recommend this, as it can lead to unecessary confusion.
我不建议这样做,因为它会导致不必要的混乱。
回答by Gabe Hollombe
Putting @ in front of a string tells the compuler not to process escape sequences found within the string.
将@ 放在字符串前面告诉计算器不要处理在字符串中找到的转义序列。
From the documentation:
从文档:
The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:
@-quoting 的好处是不处理转义序列,这样写起来很方便,例如全限定文件名:
@"c:\Docs\Source\a.txt" // rather than "c:\Docs\Source\a.txt"
To include a double quotation mark in an @-quoted string, double it:
要在 @-quoted 字符串中包含双引号,请将其加倍:
@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.
Another use of the @ symbol is to use referenced (/reference) identifiers that happen to be C# keywords. For more information, see 2.4.2 Identifiers.
@ 符号的另一个用途是使用恰巧是 C# 关键字的引用 (/reference) 标识符。有关更多信息,请参阅2.4.2 标识符。
回答by mklein
The original question asks for a reason why one would escape a not-reserved word. What comes to my mind is that if step
would become a reserved word in future the code example would still compile. I guess it is also a valid option for code generators.
最初的问题询问了为什么人们会逃避一个非保留词的原因。我想到的是,如果step
将来成为保留字,代码示例仍将编译。我想这也是代码生成器的有效选项。
回答by Stilgar
The @ sign is used with identifiers that are the same as language keywords. There are two main uses for this - interoperating with non-C# code. In languages like VB or IronPython the keywords are not the same as in C# and they may have declared classes and properties with names that match C# keywords. If this feature was not present this code would not be accessible in C#. - machine generated code. If a code generator generates code based on some external source (for example WSDL) the generator can just prefix all identifiers with @ and not check and convert identifiers that match C# keywords.
@ 符号与与语言关键字相同的标识符一起使用。这有两个主要用途 - 与非 C# 代码互操作。在 VB 或 IronPython 等语言中,关键字与 C# 中的关键字不同,它们可能已经声明了名称与 C# 关键字匹配的类和属性。如果此功能不存在,则无法在 C# 中访问此代码。- 机器生成的代码。如果代码生成器基于某些外部源(例如 WSDL)生成代码,则生成器可以只用 @ 前缀所有标识符,而不检查和转换与 C# 关键字匹配的标识符。
Are you sure that your case is not the second?
你确定你的情况不是第二个?