代表的@前缀是否具有特殊含义? [英] Does the @ prefix for delegates have any special meaning?
问题描述
几次我看到ReSharper生成的代码如下:
Several times I've seen ReSharper generate code that looks like this:
delegate void myHandler(int i);
myHandler myHandlerContainer;
...
foreach (Delegate @delegate in myHandlerContainer.GetInvocationList())
{...}
@delegate 中的 @ 是否赋予该变量任何特殊的语义含义?
或是
Does the '@' in @delegate give that variable any special semantic meaning?
Or is it just a convention I didn't encounter before?
推荐答案
MSDN的一些更多细节:
Some more details from 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.
因此,使用'@'进行前缀可以允许从一个名为delegate的类派生,可以在C#中使用另一种语言编写的库中定义。
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#.
在任何其他情况下,我不建议使用此语法而是构成与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.
另外一个有趣的事实就是在这里提到的变量命名:
There is also another interesting fact about variable naming mentioned there:
包含两个连续的
下划线字符(U + 005F)的标识符是
保留供
实现使用。例如,
实现可以提供扩展的
关键字,以两个
下划线开头。
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.
这篇关于代表的@前缀是否具有特殊含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!