Delegated notes (2)

Source: Internet
Author: User
Tags prototype definition

Previous: http://www.bkjia.com/kf/201206/134062.html

Typical examples of delegation and event:


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> 98)
{
// 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 www.2cto.com
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 + = Display. ShowMsg; // register the static method

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

Console. ReadKey ();
}
}
}
Output:

Alarm: China Xian-RealFire 001:
Alarm: Tick, the water has already reached 99 degrees:

Display: China Xian-RealFire 001:
Display: the water is boiling. The current temperature is 99 degrees.

Alarm: China Xian-RealFire 001:
Alarm: the water has reached 100 degrees:

Display: China Xian-RealFire 001:
Display: the water is boiling. The current temperature is 100 degrees.

 

At first, I didn't understand why I used the EventArgs parameter.

 

. Net Framework encoding specifications:

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.
In fact, the above is not only for coding specifications, 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.

 

 

From Fly to The Moon

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.