Delegate event (delegate and Event)

Source: Internet
Author: User
Tags prototype definition
Delegate and event in. NET Framework

Although the above example completes what we want to do, we are not only confused: Why is the event model in. NET Framework different from the above? Why are there many eventargs parameters?

Before answering the above questions, we should first understand the encoding specifications of. NET Framework:

    • The name of the delegate type should end with eventhandler.
    • Prototype definition of the delegate: There is a void returned value, and two input parameters are accepted: an object type, and an eventargs type (or inherited from eventargs ).
    • The event is named as the part left after the eventhandler is removed by the delegate.
    • The types inherited from eventargs should end with eventargs.

Next, let's explain:

    1. The parameter of the object type in the delegate declaration prototype represents the subject, that is, the monitoring object. In this example, it is the heater (water heater ). A callback function (such as makealert of alarm) can be used to access the event-triggered object (heater ).
    2. The eventargs object contains the data that the observer is interested in. In this example, It is temperature.

The above is not only for encoding standards, but also makesProgramMore flexibility.For example, if we not only want to get the temperature of the water heater, but also want to get its production date, model, and price in the observer side (alarm or display) method, the delegation and method declaration will become very troublesome. If we pass the water heater reference to the method of the alarm, we can directly access the water heater in the method.

Now we can rewrite the previous example to make it conform to. NET Framework specifications:

UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. text;

Namespace Delegate {
// Water Heater
Public Class Heater {
Private Int Temperature;
Public String Type = "Realfire 001" ; // Add a model for demonstration
Public String Area = "China Xian" ; // Add origin as demo
// Declare the delegate
Public Delegate Void Boiledeventhandler (Object sender, boiledeventargs E );
Public Event Boiledeventhandler Boiled; // Declare the event

// Define the boiledeventargs class and pass the information to the observer.
Public Class Boiledeventargs :Eventargs {
Public Readonly Int Temperature;
Public Boiledeventargs ( Int Temperature ){
This . Temperature = temperature;
}
}

// The class inherited from heater can be rewritten so that the inherited class rejects the monitoring of other objects.
Protected Virtual Void Onboiled (boiledeventargs e ){
If (Boiled! = Null ){ // If an object is registered
Boiled ( This , E );// Call the method of all registered objects
}
}

// Boil water.
Public Void Boilwater (){
For ( Int I = 0; I <= 100; I ++ ){
Temperature = I;
If (Temperature> 95 ){
// Create a boiledeventargs object.
Boiledeventargs E = New Boiledeventargs (temperature );
Onboiled (E ); // Call the onbolied Method
}
}
}
}

// Alarm
Public Class Alarm {
Public Void Makealert (Object sender, heater. boiledeventargs e ){
Heater Heater = (heater) sender;// Are you familiar with this?
// Access public fields in sender
Console . Writeline ( "Alarm: {0}-{1 }:" , Heater. Area, heater. type );
Console . Writeline ( "Alarm: Tick, the water is already {0 :" , E. Temperature );
Console . Writeline ();
}
}

// Display
Public Class Display {
Public Static Void Showmsg (Object sender, heater. boiledeventargs e ){ // Static method
Heater Heater = (heater) sender;
Console . Writeline ( "Display: {0}-{1 }:" , Heater. Area, heater. type );
Console . Writeline ( "Display: the water is boiling. The current temperature is {0. " , E. Temperature );
Console . Writeline ();
}
}

Class Program{
Static VoidMain (){
HeaterHeater =New Heater();
AlarmAlarm =New Alarm();

Heater. Boiled + = alarm. makealert; // Registration Method
Heater. Boiled + = ( New Alarm (). makealert; // Register an anonymous object
Heater. Boiled + = New Heater. boiledeventhandler (alarm. makealert ); // You can also register
Heater. Boiled + = display. showmsg; // Register the static method

Heater. boilwater (); // The method for registering an object is automatically called
}
}
}

Output:
Alarm: China Xian-realfire 001:
Alarm: The water is 96 degrees away:
Alarm: China Xian-realfire 001:
Alarm: The water is 96 degrees away:
Alarm: China Xian-realfire 001:
Alarm: The water is 96 degrees away:
Display: China Xian-realfire 001:
Display: the water is boiling. The current temperature is 96 degrees.

// Omitted

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.