C # basic enhancement-delegation, Lambda expressions, and events (Part II ),

Source: Internet
Author: User
Tags textout

C # basic enhancement-delegation, Lambda expressions, and events (Part II ),

3. Events

As a type in C #, events define the ability to send notifications for instances of classes and classes, so that events and executable code are bundled together. An event is a message sent by an object to send a notification. Operations may be caused by user interaction or by some other program logic. The object that triggers an event is called the event sender. The object that captures the event and responds to it is called the event receiver.

C # events work in the "publish-Schedule" mode. First, publish events in a class, and then you can book events in any number of classes. In event communication, the event sender class does not know which object or method will receive (process) the event it raises. It is necessary to have a media (or pointer-like mechanism) between the source and the receiver, which is the delegate.

 

The event function is provided by three interrelated elements: the class that provides event data, the class that delegates events, and the class that triggers events. If you want a class to raise an event named EventName, you need the following elements:

(1) class holding event data: EventNameEventArgs. This class must inherit the System. EventArgs class. If you do not customize a class that holds event data, use the System. EventArgs class.

(2) event Delegate: EventNameEventHandler

(3) class that triggers the event. This class must provide:

A. event Declaration: public event EventNameEventHandler EventName;

B. Method for triggering the event: OnEventName

(4) define the class that uses this event. All these classes should include:

Create an event source object. Use the defined constructor to create an object that contains the class defined by the event.

Define the event handler, that is, the method associated with the event.

Register the event source object to the event handler. Use the delegate object and the "+ =" and "-=" operators to associate one or more methods with events in the event source.

 

C # The event mechanism is implemented based on the delegate. First, you must define a delegate:

Public delegate void EventHandler (object from, myEventArgs e); // from indicates the event object

Declaration event format: Delegate name of the event name

Public event EventHandler TextOut;

Event activation is generally written as follows:

If (TextOut! = Null)

TextOut (this, new EventArgs ());

Subscription event + = evsrc. TextOut + = new EventSource. EventHandler (CatchEvent );

Unsubscribe-= evsrc. TextOut-= new EventSource. EventHandler (CatchEvent );

Here is an example:

1 public class MyEventArgs: EventArgs 2 {// defines the event containing data 3 4 private string StrText; 5 public MyEventArgs (string StrText) 6 {7 this. strText = StrText; 8} 9 public string GetStrText10 {11 get12 {13 return StrText; 14} 15} 16} 17 18 public class EventSource19 {// class of the event published 20 21 MyEventArgs EvArgs = new MyEventArgs ("trigger event "); 22 public delegate void EventHandler (object from, MyEventArgs e); // defines the delegate 23 public event EventHandler TextOut; // defines event 24 public void TriggerEvent () // activation event Method 25 {26 if (TextOut! = Null) 27 {28 TextOut (this, EvArgs); 29} 30} 31} 32 class Test33 {// class 34 static void Main (string [] args) subscribed to the event) 35 {36 EventSource evsrc = new EventSource (); 37 evsrc. textOut + = new EventSource. eventHandler (CatchEvent); // subscribes to event 38 evsrc. triggerEvent (); // trigger event 39 Console. writeLine ("--------------"); 40 41 evsrc. textOut-= new EventSource. eventHandler (CatchEvent); // cancels the subscription event 42 evsrc. triggerEvent (); // because the subscription has been canceled, no Console is executed. writeLine ("--------------"); 44 45 Test theApp = new Test (); 46 evsrc. textOut + = new EventSource. eventHandler (theApp. instanceCatch); 47 evsrc. triggerEvent (); 48} 49 private static void CatchEvent (object from, MyEventArgs e) 50 {// event handler 51 Console. writeLine ("CatchEvent: {0}", e. getStrText); 52} 53 private void InstanceCatch (object from, MyEventArgs e) 54 {// event handler 55 Console. writeLine ("InstanceCatch: {0}", e. getStrText); 56} 57}


You can refer to the introduction above to find several related elements of the event.

 

Some basic knowledge about delegation, Lamada expressions, and events ....

This is the first time I wrote a blog, and I am still exploring the dregs in college. It is almost all the content in the book. I feel very impressed when I write a word in one word, understanding is also more in place, Do not spray...

 

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.