这里的 @
是什么意思(我知道它使用的是过时的 .NET Framework 1.1 ConfigurationSettings.AppSettings)?
@ConfigurationSettings.AppSettings["some_setting"];
这不是字符串文字:Using the literal '@' with a string variable
实际代码:
_scale_id_regex = @ConfigurationSettings.AppSettings["ScaleIdRegEx"];
在作为 Windows 服务的一部分的常规 .cs 文件中,_scale_id_regex
只是类中的私有(private)字符串,因此不涉及 ASP.NET 和 Razor。
最佳答案
它称为逐字标识符。它允许您在保留字之后命名变量。例如
string @string = string.Empty;
object @object = new object();
int @int = 1;
...
您拥有的代码是有效的,但我不相信 @
在那里有任何实际用途。由于这比我刷新页面的速度更快,因此这里是 ECMA C# 语言规范第 9.4.2 节的内容。
The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.
您发布的代码是有效的,因为语言规范允许这样做,尽管不鼓励。
[Note: Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style. end note]
关于C# @ 运算符(不适用于字符串文字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11851540/