. , , .
. , ,
, . , , . . , , string :
delegate string MyDelegate();
MyDelegate, , . , d, Do . :
class Program {
delegate string MyDelegate();
static string Do() {
return "DO!";
}
static void Main() {
MyDelegate d = new MyDelegate(Do);
Console.WriteLine(d());
}
}
.NET Framework , , Action<...> Func<TResult, ...> .
, . , .
, :
event EventHandler Click;
EventHandler :
public delegate void EventHandler(Object sender, EventArgs e)
, , :
void HandleButtonClick(Object sender, EventArgs e) {
}
When you register an event handler HandleButtonClickin an event Clickusing an operator +=, it adds a delegate that points to your function to the multicast event delegate.
this.Click += HandleButtonClick;
A multi-sheeted delegate is just like a regular delegate, but it can call several functions one after another in a specific order.
When you use this event, you are actually calling the delegate to call all of these functions:
this.Click();
And now you know how events work: delegates.