For c # events,
Original link http://blog.csdn.net/joyhen/article/details/8500211
I recently looked at the delegate and then saw the event, which had been vague in the past. The association between the event trigger and the responder. Paste a simple example:
1. Create a class Control. cs
- Using System;
- Namespace EventComplex
- {
- /*
- * Event processing in C # is actually a delegate with a special signature.
- */
- Public class Control
- {
- /// <Summary>
- /// Represents the event sender
- /// </Summary>
- /// <Param name = "sender"> event sender </param>
- /// <Param name = "e"> event parameter class
- /// <Remarks> is used to contain event-related data. All event parameter classes must be derived from the System. EventArgs class. </remarks>
- /// </Param>
- Public delegate void MyEventHandler (object sender, MyEventArgs e); // System. EventArgs e
- Public event MyEventHandler MyEvent;
- Public Control ()
- {
- This. MyEvent + = new MyEventHandler (RaiseSomeEvent );
- }
- /// <Summary>
- /// An event
- /// </Summary>
- Public void RaiseSomeEvent (object sender, MyEventArgs e)
- {
- Console. WriteLine ("hello" + e. ParamName );
- Console. ReadLine ();
- }
- Public void RiseSomeEvent ()
- {
- // System. EventArgs e = new System. EventArgs ();
- Console. Write ("Please input 'name ':");
- String _ param = Console. ReadLine (); // ReadLine: Press enter to read the data and then trigger the MyEvent event.
- // If (_ param. Equals ("jon") // triggers the event when the user inputs jon. Otherwise, the event is not triggered.
- MyEvent (this, new MyEventArgs (_ param); // MyEvent (this, e)
- }
- }
- Public class MyEventArgs: System. EventArgs
- {
- Public string ParamName;
- Public MyEventArgs (string _ name)
- {
- ParamName = _ name;
- }
- }
- }
2. Let's test it in the Main function;
- Public static void demo4 ()
- {
- Control ctr = new Control ();
- Ctr. MyEvent + = new Control. MyEventHandler (ResponseSomeEvent );
- Ctr. RiseSomeEvent ();
- }
- Public static void ResponseSomeEvent (object sender, EventArgs e)
- {
- Console. WriteLine ("Some event occur! ");
- }
- Static void Main (string [] args)
- {
- Demo4 ();
- }