Delegate and event in C # (6)-delegate and event in. Net Framework

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:

  1. The name of the delegate type should end with EventHandler.
  2. 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 ).
  3. The event is named as the part left after the EventHandler is removed by the delegate.
  4. The types inherited from EventArgs should end with EventArgs.

Further instructions:

  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.

In fact, the above is not only for encoding standards, but also makes the program more flexible.. 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:

Using System;
Using System. Collections. Generic;
Using System. 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 the origin as a demo
// Declare the delegate
Public delegate void BoiledEventHandler (Object sender, BoliedEventArgs e );
Public event BoiledEventHandler Boiled; // declare the event

// Define the BoliedEventArgs class and pass the information to the Observer.
Public class BoliedEventArgs: EventArgs {
Public readonly int temperature;
Public BoliedEventArgs (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 OnBolied (BoliedEventArgs 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 BoliedEventArgs object.
BoliedEventArgs e = new BoliedEventArgs (temperature );
OnBolied (e); // call the OnBolied Method
}
}
}
}

// Alarm
Public class Alarm {
Public void MakeAlert (Object sender, Heater. BoliedEventArgs 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, water has {0} degrees:", e. temperature );
Console. WriteLine ();
}

}

// Display
Public class Display {
Public static void ShowMsg (Object sender, Heater. BoliedEventArgs e) {// static method
Heater heater = (Heater) sender;
Console. WriteLine ("Display: {0}-{1}:", heater. area, heater. type );
Console. WriteLine ("Display: The water is almost burned out. Current temperature: {0. ", E. temperature );
Console. WriteLine ();
}
}

Class Program {
Static void Main (){
Heater heater = new Heater ();
Alarm alarm = new Alarm ();

Heater. Boiled + = alarm. MakeAlert; // Registration Method
Heater. Boiled + = (new Alarm (). MakeAlert; // Method for registering anonymous objects
Heater. Boiled + = new Heater. BoiledEventHandler (alarm. MakeAlert); // You can also register
Heater. Boiled + = Display. ShowMsg; // register the static method

Heater. BoilWater (); // The method of boiling water, which automatically calls the method of registering an object
}
}
}

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.
// Omit...

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.