Delegates and Events

Delegates are the type-safe, secure, managed equivalents of C++ function pointers. You can use delegates with instance, virtual, or static methods. Another way to think about delegates is as encapsulating a specific method. In .NET, delegates are also used for events and callback functions.

Delegates are implemented by inheriting from System.Delegate and supporting a list of methods executed when the delegates are invoked. Listing A.27 shows the use of a delegate.

Listing A.27. Delegates
 1: using System; 2: 3: delegate void MessageDelegate(); 4: 5: public class MyClass{ 6: public void WriteMessage (){ 7: Console.WriteLine("I made this."); 8: } 9: } 10: 11: public class MyClassTester{ 12: static public void Main (){ 13: MyClass ...

Get Building e-Commerce Sites with the .NET Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.