Thursday, June 24, 2010

Verbatim Identifiers in C#

Not that many people realize that in C#, one can use language keyword names like "void" and "bool" for their identifiers.
The trick is, you need to prefix the name i.e.:

 void @void() {}

declares method named "void".

Of course you would rarely use this feature, if ever but many people start arguing "Why Microsoft did this to begin with?"

I am personally 100% on-board with MS on this one, - not only this is a good practice to be consistent in language design (less limitations) by allowing programmer to use any name
 but this is a must-have feature i.e. you may code an assembly in Delphi Prism( as an example) and call a method "void" -
 then you'd need to consume it in C#. "void" is not a keyword in that language.

I personally think that purposely naming you stuff with "dangerous" names is a bad thing, but having verbatim name capability makes us feel more liberated :)