Recently, I was reading C# In Depth, By Jon Skeet. It is one of the best books about learn advanced details about C#. While reading the book, I decided to prepare notes. So, that I can just go through the notes to revise, If I have to. Following are the points about the delegates that I read in the book.

  1. Delegate is a strongly typed function pointer (Simple but vague)
  2. Delegate term is often used for delegate type as well as instance. So, better to be clear is it delegate type or delegate instance.
  3. Delegate type is like an interface with a method
  4. Delegate type is just another type in C#. A delegate type inherits System.MulticastDelegate which in turn inherits from System.Delegate
  5. Delegate instance has a list of actions/methods (Multicast Delegate). This is called invocation list.
  6. Delegate type uses 2 methods of System.Delegate Class called “Combine” and “Remove” to add actions/methods as well as remove actions/methods
  7. Delegate instance are immutable like string, each addition(+ or +=) causes creation of one more instance and actions are copied over. Same is true for removal using – or -= which in turn calls Remove.
  8. “Invoke” method causes the invocation of all the actions/methods in the invocation list
  9. All of the actions are executed in order. But the value returned is the value returned by the last action, or if you wish to have return value of each method/action then invoke using “Delegate.GetInvocationList”
  10. If any one methods in invocation list throws an exception, Invocation is stopped and remaining methods/actions are not invoked/executed.