加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 代表的@前缀是否具有特殊含义?

发布时间:2020-12-15 06:57:45 所属栏目:百科 来源:网络整理
导读:几次我看到ReSharper生成的代码如下所示: delegate void myHandler(int i);myHandler myHandlerContainer;...foreach (Delegate @delegate in myHandlerContainer.GetInvocationList()){...} @delegate中的“@”是否赋予该变量任何特殊的语义? 还是只是一
几次我看到ReSharper生成的代码如下所示:
delegate void myHandler(int i);
myHandler myHandlerContainer;
...
foreach (Delegate @delegate in myHandlerContainer.GetInvocationList())
{...}

@delegate中的“@”是否赋予该变量任何特殊的语义?
还是只是一个我以前没有遇到的惯例?

解决方法

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# Language Specification: 2.4.2 Identifiers.

因此,使用’@’进行前缀可以例如.从一个名为“delegate”的类派生,这个类可能在用C#编写的另一种语言的库中定义.

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

还有一个有趣的事实,关于在这里提到的变量命名:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读