Calling Invoke on a Delegate

In C# version 1.x you could not call Invoke directly on a delegate instance. Doing so would generate a compiler error.

class MyApp
  delegate void MyDelegate();
  static void TargetMethod(){}

  static void Main(string[] args)
    MyDelegate del = new MyDelegate(TargetMethod);

    // Illegal in C# 1.x — generates a CS1533 compiler error

The way to invoke a delegate was to use the delegate like calling a method directly.

// In C# 1.x you must invoke a delegate like this

Not everyone liked having to do it this way. (Don Box was critical of this requirement in his book, Essential .Net Volume 1.) Visual Basic .Net allowed calling Invoke on the delegate instance, which seems more intuitive.

Well, guess what!? The C# design team listened and gave us the ability to call Invoke on the delegate instance in version 2.0!

// C# 2.0 allows us to call Invoke on the delegate instance!

I discovered this change just by accident (chalk it up to my insatiable curiosity), and a quick Google search did not return any relevant results. Even the MSDN Visual Studio documentation is mute on the point. I wonder if I’m the first one to have noticed? Sort of makes me feel like the first person to discover that the earth is round (well, maybe that’s going too far). B)

About Tony Sneed

Sr. Software Solutions Architect, Hilti Global Application Software
This entry was posted in Technical. Bookmark the permalink.