是否为代表@前缀有什么特殊的含义? [英] Does the @ prefix for delegates have any special meaning?

查看:113
本文介绍了是否为代表@前缀有什么特殊的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有好几次我看到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.

C#语言规范:2.4.2标识符<因此/ A>

from C# Language Specification: 2.4.2 Identifiers.

用前缀'@'允许如从命名可能以书面比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#的关键字不同的标识符(如VALU而不是值)增加代码的可读性和避免混淆,是否有连接到它的任何特殊含义。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆