14

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. I haven't come across this before and can't seem to find any documentation for it.

What does this mean? Is it useful?

1
  • 4
    See msdn.microsoft.com/en-us/library/x53a06bb%28v=vs.71%29.aspx: "Keywords are predefined reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a legal identifier but if is not because it is a keyword."
    – Otiel
    Nov 9, 2011 at 17:16

5 Answers 5

24

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. However prefix it with an @ character and it no longer is a keyword, just a valid variable name!

1
9

@ allows you to use reserved keywords as the name of a parameter.

6

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.

4

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#.
  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.
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.