代表的@前缀是否有任何特殊含义?

时间:2022-07-12 22:35:06

Several times I've seen ReSharper generate code that looks like this:

有几次我见过ReSharper生成的代码如下所示:

delegate void myHandler(int i);
myHandler myHandlerContainer;
...
foreach (Delegate @delegate in myHandlerContainer.GetInvocationList())
{...}

Does the '@' in @delegate give that variable any special semantic meaning?
Or is it just a convention I didn't encounter before?

@delegate中的'@'是否赋予该变量任何特殊的语义含义?或者这只是我之前没遇到过的惯例?

2 个解决方案

#1


Some more details from MSDN:

来自MSDN的更多细节:

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. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

前缀“@”允许使用关键字作为标识符,这在与其他编程语言交互时很有用。字符@实际上不是标识符的一部分,因此标识符可能在其他语言中看作普通标识符,没有前缀。带有@前缀的标识符称为逐字标识符。允许使用@前缀作为非关键字的标识符,但强烈建议不要使用样式。

from C# Language Specification: 2.4.2 Identifiers.

来自C#语言规范:2.4.2标识符。

Prefixing with '@' therefore allows e.g. to derive from a class named "delegate" which might be defined in a library written in another language than C#.

因此,使用'@'进行前缀允许例如从名为“delegate”的类派生,该类可能在用C#以外的其他语言编写的库中定义。

In any other case I would not recommend to use this syntax and rather make up identifiers different from the C# keywords (e.g. valu instead of value) to increase code readability and avoid confusion whether there is any special meaning attached to it.

在任何其他情况下,我不建议使用此语法,而是构建与C#关键字不同的标识符(例如,值而不是值),以提高代码可读性,并避免混淆是否附加任何特殊含义。

There is also another interesting fact about variable naming mentioned there:

关于变量命名还有另一个有趣的事实:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.

包含两个连续下划线字符(U + 005F)的标识符保留供实现使用。例如,实现可能会提供以两个下划线开头的扩展关键字。

#2


The "@delegate" is to differentiate the variable name from the "delegate" keyword.

“@delegate”用于区分变量名称和“delegate”关键字。

#1


Some more details from MSDN:

来自MSDN的更多细节:

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. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

前缀“@”允许使用关键字作为标识符,这在与其他编程语言交互时很有用。字符@实际上不是标识符的一部分,因此标识符可能在其他语言中看作普通标识符,没有前缀。带有@前缀的标识符称为逐字标识符。允许使用@前缀作为非关键字的标识符,但强烈建议不要使用样式。

from C# Language Specification: 2.4.2 Identifiers.

来自C#语言规范:2.4.2标识符。

Prefixing with '@' therefore allows e.g. to derive from a class named "delegate" which might be defined in a library written in another language than C#.

因此,使用'@'进行前缀允许例如从名为“delegate”的类派生,该类可能在用C#以外的其他语言编写的库中定义。

In any other case I would not recommend to use this syntax and rather make up identifiers different from the C# keywords (e.g. valu instead of value) to increase code readability and avoid confusion whether there is any special meaning attached to it.

在任何其他情况下,我不建议使用此语法,而是构建与C#关键字不同的标识符(例如,值而不是值),以提高代码可读性,并避免混淆是否附加任何特殊含义。

There is also another interesting fact about variable naming mentioned there:

关于变量命名还有另一个有趣的事实:

Identifiers containing two consecutive underscore characters (U+005F) are reserved for use by the implementation. For example, an implementation might provide extended keywords that begin with two underscores.

包含两个连续下划线字符(U + 005F)的标识符保留供实现使用。例如,实现可能会提供以两个下划线开头的扩展关键字。

#2


The "@delegate" is to differentiate the variable name from the "delegate" keyword.

“@delegate”用于区分变量名称和“delegate”关键字。