Events in. net

Source: Internet
Author: User

This article is a classic record of learning C # advanced programming and C # getting started.

To understand the event, you must first authorize it. The event is based on delegation. Events are delegated by the publishing/subscription mechanism.

Events are triggered by objects. We can provideCodeTo process events. We need to subscribe to (subscribe) Events in the code to execute the code when the event occurs, that is, set the event processingProgram.

A single event can have multiple time handlers. After this event occurs, these handlers will be called. The time handler can be in the class of the object that raises the event, or in other classes. The event handler itself is a simple method. The only requirement on the event handling method is that it must match the response type and parameters required by the event.

The basic process of event processing is as follows. First, the application creates an object that can trigger events. Then, the application subscribes to the event. When an event is triggered, the subscriber is notified to execute the corresponding time processing program.

The following is an example of an event that directly connects the event publisher and the subscriber. This connection method is called forced connection. In this example, the cardealer class provides an event triggered when a new car arrives. The customer class subscribes to this event to receive notifications when the new car arrives.

First, define the event Publisher:

 Public   Class Carinfoeventargs: eventargs// As the event parameter, it must be derived from the eventargs base class { Public Carinfoeventargs ( String Car ){ This . Car = car ;} Public   String Car { Get ; Private   Set ;}} Public   Class Cardealer { Public   Event Eventhandler <carinfoeventargs> newcarinfo; // Define the event        Public   Void Newcar ( String Car) {console. writeline (" Cardealer, new car {0} ", Car ); If (Newcarinfo! = Null ) {Newcarinfo ( This , New Carinfoeventargs (CAR )); // Trigger the event }}}
The cardealer class uses the event keyword to define the event newcarinfo of the class type eventhandler <carinfoeventargs>. This event is triggered in the newcar method.

An event generally uses a method with two parameters. The first parameter is an object that contains the event publisher. The second parameter provides information about a class event. The second parameter varies with the event type. However, the type of the second parameter must be derived from the eventargs class. This is an agreement. Only those programs that meet these requirements can be used as event handlers.

Step 2: subscribe to this event in the Class Customer

 
Public ClassConsumer {Private StringName;PublicConsumer (StringName ){This. Name = Name ;}Public VoidNewcarishere (ObjectSender, carinfoeventargs e) {console. writeline ("{0}: Car {1} is new", Name, E. Car );}}

 

The customer class is used for event listening. This class subscribes to the time of the cardealer class. And defines the newcarishere method that meets the eventhandler <carinfoeventargs> delegate requirements

Then you can connect the event publisher and event subscriber:

 
Static VoidMain () {var dealer =NewCardealer (); var Michael =NewConsumer ("Michael"); Dealer. newcarinfo + = Michael. newcarishere; dealer. newcar ("Mercedes"); Var Nick =NewConsumer ("Nick"); Dealer. newcarinfo + = Nick. newcarishere; dealer. newcar ("Ferrari"); Dealer. newcarinfo-= Michael. newcarishere; dealer. newcar ("Toyota");}

 

When running this program, Michael first subscribes to the newcarinfo event (dealer. newcarinfo + = Michael. newcarishere;). When a Mercedes arrives, Michael will be notified.

Nick also subscribes to this event (dealer. newcarinfo + = Nick. newcarishere;). When a Ferrari arrives, both Michael and Nick will be notified.

Michael later canceled his subscription to this event (dealer. newcarinfo-= Michael. newcarishere;). When a Toyota arrives, Nick will be notified, but Michael will not be notified.

So far, the entire process of event processing has been completed.

In this example, the publisher and subscriber are connected directly through events. However, this connection method may cause some difficulties for garbage collection. To solve this problem, we need to use the weak event mode. That is, weekeventmanager is used as the intermediary between the event publisher and the listener. This is a little troublesome. I don't understand it very well. I will not write it first.

 

Summary:

1. events are a type of delegation. I understand the event as a system-defined eventhandler <t> wildcard delegate. The two parameter types required for this delegate are object and T. T must be derived from the base class eventargs

2. The event processing process is publishing-subscription mode.

3. You can create a subscription and cancel the subscription through + = and-=.

 

Finally, I wish you a happy New Year. I wish myself a programmer in the New Year.

Technorati label: Event

 

 

On July 11, December 3, the Code display was edited.

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.