Jef Claes

On software and life

11 Apr 2011

Using C# keywords as variables

Hold it, don’t shoot me. I know this would be an awful practice, but it is an interesting C# compiler quirk nonetheless.

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.

static void Main(string[] args) {           
    var @if = "oh my..";

    Console.WriteLine(@if);
}
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========