Button1.Click + = new EventHandler (button#click); --------------- @ 1
A piece of code that everyone is familiar with. Button1.Click is an event (or a delegate chain), EventHandler is a delegate, button#click is the subscriber of an event, or a delegate.
Through this mechanism, an event can have multiple subscribers, so that you can click a button to respond to multiple methods.
Delegate, as the name implies, to others.
Event. In the "Publisher/subscriber" mode, the publisher publishes an event. The Subscriber subscribes to this event and notifies the subscriber when the event is triggered. The notification process is implemented through delegation.
Let's imagine this scenario,ManagerThere isProjectPlan A is in charge of the artist, and B is in charge of the program and divides the project into two parts. So one day, I told them the plan when I was dining.
Publisher: Manager --------- @ 2
Reasons for subscriber to subscribe to this event: A and B are managers ------------- @ 3
Event: the manager assigned A and B Project ------------- @ 4
Cause of event triggering: the manager has a plan -------------- @ 5
Notification method (delegated): "tell" method -------------- @ 6 when dining
Public delegate void OneThing (object sender, CustomEventArgs e); defines a delegate. The returned value is null. The delegate name OneThing is equivalent to EventHandler in @ 1, there are two parameters: the object that triggers the event and the event information.
CustomEventArgs must inherit from EventArgs
Public class CustomEventArgs: EventArgs
{
Public CustomEventArgs () // Constructor
{}
}
Declare the event public event OneThing DoThing;
Define the cause of an event: If the input text is "you two have finished this thing" (@ 5), the event will be triggered.
Define a Text attribute
Public string Text
{
Get
{
Return _ text;
}
Set
{
_ Text = value;
If (_ text = "you two have finished this ")
This. DoThing (this, new CustomEventArgs ());
Else
Console. WriteLine ("You have not triggered the event ");
}
}
Once an event is triggered, the subscriber must be notified through the delegate mechanism.
DelegateExample de = new DelegateExample (); // DelegateExample class. For details, see the source code.
De. DoThing + = new DelegateExample. OneThing (de. DoHalfThing );
De. DoThing + = new DelegateExample. OneThing (de. DoOtherHalfThing );
When DoThing is triggered, it is delegated to the DohalfThing and DoOtherHalfThing functions.
Write the function body for the two delegate Methods
Public void DoHalfThing (object sender, DelegateExample. CustomEventArgs e)
{
Console. WriteLine ("okay, I only do half, and the other half do it for others ");
}
Public void DoOtherHalfThing (object sender, DelegateExample. CustomEventArgs e)
{
Console. WriteLine ("Okay, the other half will be handed over to me ");
}
Note that the parameters must be the same as when the delegate is declared.
A general process is,
Enter "you two have finished this ",
Trigger event DoThing
Because DoThing registers two functions for event processing (delegated. Therefore, two functions are executed.
All code
Class DelegateExample
{
String _ text;
Public delegate void OneThing (object sender, CustomEventArgs e );
Public event OneThing DoThing;
Public string Text
{
Get
{
Return _ text;
}
Set
{
_ Text = value;
If (_ text = "you two have finished this ")
This. DoThing (this, new CustomEventArgs ());
Else
Console. WriteLine ("You have not triggered the event ");
}
}
Public class CustomEventArgs: EventArgs
{
Public CustomEventArgs ()
{}
}
Public void DoHalfThing (object sender, CustomEventArgs e)
{
Console. WriteLine ("okay, I only do half, and the other half do it for others ");
}
Public void DoOtherHalfThing (object sender, CustomEventArgs e)
{
Console. WriteLine ("Okay, the other half will be handed over to me ");
}
}
Static void Main (string [] args)
{
# Region delegation and events
DelegateExample de = new DelegateExample ();
De. DoThing + = new DelegateExample. OneThing (de. DoHalfThing );
De. DoThing + = new DelegateExample. OneThing (de. DoOtherHalfThing );
String input = Console. ReadLine ();
De. Text = input;
Console. ReadLine ();
# Endregion
}