What is the @ sign before a variable name

You probably saw this at least once

var @delegate = ...

Or especially in generated code (like if you let R# to do some rewriting for you) you can see

var @t = ...

So, what is it? the simple answer is the ‘@’ sign can be added to a reserved word and then you can define for example variable that with the name “@delegate” but you can not define “delegate” variable.

But what special with ‘@’ ? you can add any other characters like “_delegate”?

It’s indeed different because when you add the ‘@’ sign, in the assembly the variable name is saved without the ‘@’ sign, so “@delegate” will be just “delegate”.

It can see meaningless until you will be in situation that it is matter, and this is exactly what happened to me.

I have a code that create a type with property named “@t”, then I try to get the property by name with reflection.

All of this is part of a huge generated code that is very hard to read or debug.

When I got a NRE I figured it out that is because there is no “@t” property, but this is weird because I know the name is “@t”!

With the knowing the above, I get the property with the name excluding the ‘@’ sign and everything works fine.

This entry was posted in .NET, c# and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.