35

I've just added a weak event implementation to a project using Dustin Campbell's WeakEvent class. Although blindly using Code I Found On The Internet™ is generally a bad idea, it's a far better implementation than what I previously hacked together. It seems to work well so far, but in an effort to understand the code I came across the following:

public class WeakEventHandler<T, E> : IWeakEventHandler<E>
    where T : class
    where E : EventArgs
{
    private delegate void OpenEventHandler(T @this, object sender, E e);
    ...

I'm used to declaring delegates types with just the object sender and EventArgs args arguments, so what does the T @this part achieve? Obviously it is declaring something of WeakEventHandler's T generic type but I've never seen @this before (and googling it is understandably hopeless).

1

2 Answers 2

42

The @this means you can use the keyword this as a variable.

The T is simply the first open generic type of WeakEventHandler<T, E>.

6
  • 1
    So it just lets you use a reserved word as a variable name? Commented Apr 4, 2011 at 9:16
  • 5
    Yes, just like @event, @class or @object. Commented Apr 4, 2011 at 9:17
  • 2
    Just a little warning, it's also the literal string designator, so eg you can write @"debug\Myfolder" without it thinking the backslash is an escape char.
    – Carlos
    Commented Apr 4, 2011 at 9:20
  • 1
    @Carlos - not in front of a variable name, it isn't. Yes, it is indeed contextual and has different meaning before strings.
    – Oded
    Commented Apr 4, 2011 at 9:21
  • 3
    @Oded. Yes, I meant to provide the other usage to prevent confusion. Don't know if I managed that...
    – Carlos
    Commented Apr 5, 2011 at 7:38
40

The @ symbol allows you to escape identifiers within your code.

See MSDN -

The rules for identifiers given in this section correspond exactly to those recommended by the Unicode Standard Annex 15, except that underscore is allowed as an initial character (as is traditional in the C programming language), Unicode escape sequences are permitted in identifiers, and the "@" character is allowed as a prefix to enable keywords to be used as identifiers.

http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx

They give this lovely example of escaping:

class @class
{
   public static void @static(bool @bool) {
      if (@bool)
         System.Console.WriteLine("true");
      else
         System.Console.WriteLine("false");
   }   
}

Would like to see that one in a code review!

2
  • 6
    Thanks @Stuart, yes it looks like something to avoid! Commented Apr 4, 2011 at 9:28
  • It makes more sense when you use the code in other languages such as VB.Net. "this" isn't special in that language. Intellisense would display it as "this"
    – mark_h
    Commented Jun 29, 2018 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.