C# Delegates Steps


This entry is part 6 of 8 in the series C# Delegates

The book Illustrated C# 7, Fifth Edition by Daniel Solis and Cal Schrotenboer published by Apress shows the steps involved in using a delegate, and illustrates that they are very similar to the steps you use to work with custom classes.

As the book says on page 350: “A delegate is a user-defined type, just as a class is a user-defined type. But whereas a class represents a collection of data and methods, a delegate holds one or more methods and a set of predefined operations.”

  1. Declare a delegate type. A delegate declaration looks like a method declaration, except that it doesn’t have an implementation block.
  2. Declare a delegate variable of the delegate type.
  3. Create an object of the delegate type and assign it to the delegate variable. The new delegate object includes a reference to a method that must have the same signature and return type as the delegate type defined in the first step.
  4. You can optionally add additional methods into the delegate object. These methods must have the same signature and return type as the delegate type defined in the first step.
  5. Throughout your code you can then invoke the delegate, just as it if it were a method. When you invoke the delegate, each of the methods it contains is executed.
Series Navigation<< C# Delegates GeneralC# Delegates Illustrated Example >>