by Nick
Tuesday, June 07, 2005 10:16 AM
Chris Sells points writes about a piece of C# code that freaked him out:
class Class1 {
static void Foo(object @this) {
Console.WriteLine(@this);
} static void Main(string[] args) {
Foo("hi");
}
}
In case you don't quite see it... a keyword (this) is being used as a variable to represent something other than the current object. You can do this because placing the @ before a variable tells the compiler to ignore it as a keyword. It's kind of cool, but also confusing I think. VB has a similar ability if you wrap the variable in square brackets (for instance you can name something [Me]).
I knew about the square brackets in VB, but only thought an @ in C# could be used in front of a literal string. You learn something new every day.