Overview of delegates and events
In this section, we will discuss the basics of delegates and events. Both delegates and events are the most advanced features of the C# language. We will understand these in coming sections in detail.
Delegates
In C#, delegates are a similar concept to pointers to functions, as in C and C++. A delegate is nothing but a variable of a reference type, which holds a reference of a method, and this method is triggered.
Note
We can achieve late binding using delegates. In Chapter 7, Understanding Object Oriented Programing with C#, we will discuss late binding in detail.
System.Delegate
is a class from which all delegates are derived. We use delegates to implement events.
Declaring a delegate type
Declaring a delegate type is similar to the method signature class. We just need to declare a type public delegate string: PrintFizzBuzz(int number);
. In the preceding code, we declared a delegate type. This declaration is similar to an abstract method with the difference that...