Delegates
Delegates provide a late binding mechanism in .NET. Late Binding means that you create an algorithm where the caller also supplies at least one method that implements part of the algorithm.
Define delegate types
Let's start with the 'delegate' keyword, because that's primarily what you will use as you work with delegates. The code that the compiler generates when you use the delegate
keyword will map to method calls that invoke members of the Delegate and MulticastDelegate classes.
You define a delegate type using syntax that is similar to defining a method signature. You just add the delegate
keyword to the definition.
Let's continue to use the List.Sort() method as our example. The first step is to create a type for the comparison delegate:
Un delegado es un tipo que representa referencias a métodos con una lista de parámetros determinados y un tipo de valor devuelto.
Delegates are like C ++ function pointers, but they are type-safe.
Delegates allow methods to be passed as parameters.
Delegates can use to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on a single event.
The methods do not have to exactly match the delegate type.
Last updated