14.1. What are delegates?

Think of a delegate as a special type – something like a class. [2] A delegate type is a class type that is derived from System.Delegate. When you declare a delegate using the delegate keyword, you do not have to specify that you are deriving from System.Delegate; that is implicit.

[2] A note for C++ developers: C# delegates are similar to C++ function pointers. However, unlike function pointers, delegates are object-oriented.

A delegate type's main job is to encapsulate one or more methods (see Figure 14.1). A delegate instance is an instance of a delegate type. You can view delegate instances as representations of methods, so that when you invoke a delegate instance, the method(s) it encapsulates is also invoked.

Get From Java to C#: A Developer's Guide 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.