Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# strudel sign

While coding in C#, I by mistake added a strudel sign before a variable in if statement (instead of exclamation mark).

bool b = false;
if (@b)
{

}

I surprised it compiled successfully without any error.

I wonder: What is the meaning of the above code?

like image 536
DxCK Avatar asked Sep 26 '10 11:09

DxCK


1 Answers

@ can be used to "escape" identifiers, in case you want to use keywords. For example:

int @class = 10;

Of course it's usually a bad idea to use keywords as identifiers, but if you're using a class library which happens to use them, it can be hard to avoid. It can also be useful sometimes to use "@this" for situations where you want to effectively have a this reference, but for whatever reason you can't use one. (These are pretty few and far between, but I've seen it a couple of times, and it's worth at least knowing about.)

like image 70
Jon Skeet Avatar answered Oct 11 '22 00:10

Jon Skeet