简体   繁体   English

对象@object是什么意思

[英]What does object @object mean

I've been playing around with events and delegates and need to raise my event asynchronously, thus I've been using: 我一直在玩事件和代表,需要异步提升我的事件,因此我一直在使用:

public event EventHandler OnHelloEvent;

public void Raise()
{
    IAsyncResult syncResult = OnHelloEvent.BeginInvoke(this, new EventArgs(), null, null)

In Intellisense, the last null is stated to be object @object . 在Intellisense中,最后一个null被声明为object @object I haven't come across this before and can't seem to find any documentation for it. 我之前没有遇到这个,似乎无法找到任何文档。

What does this mean? 这是什么意思? Is it useful? 它有用吗?

The @ sign can be thought of as "escape" character of sorts. @符号可以被认为是各种各样的“逃脱”字符。 Since object is a keyword in C#, you cannot use it as a variable name. 由于object是C#中的关键字,因此不能将其用作变量名。 However prefix it with an @ character and it no longer is a keyword, just a valid variable name! 但是用@字符作为前缀,它不再是关键字,只是一个有效的变量名!

@允许您使用保留关键字作为参数的名称。

That is the special character for escaping reserved words so that they can be used as identifiers. 这是转义保留字的特殊字符,以便它们可以用作标识符。

See Section 2.4.2 of the specification . 参见规范的第2.4.2节

Everyone answered "What does this mean?" 每个人都回答“这是什么意思?” but nobody answered "Is it useful?" 但没有人回答“它有用吗?”

In most cases, the answer is No . 在大多数情况下,答案是否定的 You should not use this. 应该使用这个。

There are a few special exceptions. 有一些特殊例外。 Off the top of my head: 脱离我的头顶:

  1. Interoperability issues with someone else's code: Someone else's code requires you to have a variable with a name of a reserved word. 与其他人的代码的互操作性问题:其他人的代码要求您拥有一个名为保留字的变量。 Maybe their code was written in a language with different reserved words than C#. 也许他们的代码是用不同于C#的保留字的语言编写的。
  2. Computer-generated code: It doesn't hurt to use an @ symbol. 计算机生成的代码:使用@符号没有坏处 If you're paranoid about reserved word collisions, you might decide that all variables in your computer-generated code will use an @ symbol. 如果你对保留字冲突有偏执,你可能会认为计算机生成代码中的所有变量都使用@符号。 Or maybe you are allowing a non-C# program to generate C# programs via a scripting language or whatever and you want to support variables named class . 或者您可能允许非C#程序通过脚本语言或其他任何方式生成C#程序,并且您希望支持名为class变量。

The @ symbol is just a prefix which allows you to use a reserved identifier as a variable name. @符号只是一个前缀,允许您使用保留的标识符作为变量名称。

So object @object , defines a variable of type object called object. 因此object @object定义了一个名为object的对象类型的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM