@ prefix for C# names

Who’d have thought I’d learn something over Twitter (during an uncharacteristic period of uptime)? Nothing overly useful mind you, just an interesting bit of trivia. :)

Besides "yuck", what would you think when you saw the following C# code?

class @class {
  public string @someValue;
  public int @int;
}

[Test]
public void TestAtClass() {
  @class myclass = new @class();
  myclass.@int = 10;
  myclass.@someValue = "test";
}

Prior to today I’d have thought that it wouldn’t compile, but after seeing a tweet from Brad Wilson that mentioned you could use @ to prefix variable names I thought I’d give it a try, and sure enough the C# compiler is happy. Brad’s tweet was about using @ to prefix reserved words so you can get names like @class.

Bit evil perhaps, but all those @’s might make some Rubyists feel a bit more at home in C# ;-)

Comments