C# @ 运算符(不适用于字符串文字)

标签 c#

这里的 @ 是什么意思(我知道它使用的是过时的 .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/

相关文章:

c# - 即使在配置文件中使用 .NET 4.0 后,nunit 代理也会挂起

c# - gZipBuffer 压缩从 C# 转换为 F#

C# Mono ssl 问题

c# - C# 中的解析器。如何从字符串填充类

c# - 从没有 regsvr32 的文件创建 COM 对象

c# - OrderBy/ThenBy 循环 - C# 中的嵌套列表

c# - 使用任务库的 Amazon S3 异步上传

c# - jQuery.validate 忽略了我的规则声明

c# - 使用 UWP 替换 XML 文件或删除 XML 文件中的元素

c# - 在c#代码中可以使用斜杠作为路径吗