@this and that

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.

Comments are closed

About Me

Nick Schweitzer Nick Schweitzer
Wauwatosa, WI

Contact Me
I'm a Software Consultant in the Milwaukee area. Among various geeky pursuits, I'm also an amateur triathlete, and enjoy rock climbing. I also like to think I'm a political pundit. ... Full Bio

Community Involvement

Twitter

Archives

Flickr Photos

www.flickr.com
This is a Flickr badge showing public photos and videos from Nick_Schweitzer. Make your own badge here.

Standard Disclaimers

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 Nick Schweitzer