Summary
A delegate is useful in order to encapsulate a method. It is like any data type in C# in which a variable can be initialized to have the delegate data type. Since it similar to data types, increment and decrement operations can be applied to the delegate, making it possible to create a multicast delegate from several delegates. However, one thing to remember, since the Delegate.Combine()
and Delegate.Remove()
methods return the Delegate
data type, is that we have to cast the return of both methods to the expected instance delegate when using them. Compared to the +=
and -=
operators use, however, since they are implemented at the language level in the compiler and the delegate type is known, there's no need to cast the result of the increment and decrement delegate operation.
C# also has built-in delegates, Action
and Func
, which make the code shorter, and the definition of the delegate becomes easier and quicker. As a result, the code gets simpler to be analyzed. Also, there...