For c # events,

Source: Internet
Author: User

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

 

  1. Using System;
  2. Namespace EventComplex
  3. {
  4. /*
  5. * Event processing in C # is actually a delegate with a special signature.
  6. */
  7. Public class Control
  8. {
  9. /// <Summary>
  10. /// Represents the event sender
  11. /// </Summary>
  12. /// <Param name = "sender"> event sender </param>
  13. /// <Param name = "e"> event parameter class
  14. /// <Remarks> is used to contain event-related data. All event parameter classes must be derived from the System. EventArgs class. </remarks>
  15. /// </Param>
  16. Public delegate void MyEventHandler (object sender, MyEventArgs e); // System. EventArgs e
  17. Public event MyEventHandler MyEvent;
  18. Public Control ()
  19. {
  20. This. MyEvent + = new MyEventHandler (RaiseSomeEvent );
  21. }
  22. /// <Summary>
  23. /// An event
  24. /// </Summary>
  25. Public void RaiseSomeEvent (object sender, MyEventArgs e)
  26. {
  27. Console. WriteLine ("hello" + e. ParamName );
  28. Console. ReadLine ();
  29. }
  30. Public void RiseSomeEvent ()
  31. {
  32. // System. EventArgs e = new System. EventArgs ();
  33. Console. Write ("Please input 'name ':");
  34. String _ param = Console. ReadLine (); // ReadLine: Press enter to read the data and then trigger the MyEvent event.
  35. // If (_ param. Equals ("jon") // triggers the event when the user inputs jon. Otherwise, the event is not triggered.
  36. MyEvent (this, new MyEventArgs (_ param); // MyEvent (this, e)
  37. }
  38. }
  39. Public class MyEventArgs: System. EventArgs
  40. {
  41. Public string ParamName;
  42. Public MyEventArgs (string _ name)
  43. {
  44. ParamName = _ name;
  45. }
  46. }
  47. }


2. Let's test it in the Main function;

 

 

  1. Public static void demo4 ()
  2. {
  3. Control ctr = new Control ();
  4. Ctr. MyEvent + = new Control. MyEventHandler (ResponseSomeEvent );
  5. Ctr. RiseSomeEvent ();
  6. }
  7. Public static void ResponseSomeEvent (object sender, EventArgs e)
  8. {
  9. Console. WriteLine ("Some event occur! ");
  10. }

 

  1. Static void Main (string [] args)
  2. {
  3. Demo4 ();
  4. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.