EventHandler in C,
// A water tank class is defined here
Public class water tank
{
// This is the water tank flushing operation
Public void drain (){}
// This is the property of the water tank.
Public double volume;
// This is an empty water tank event.
Public event EventHandler tank is empty;
}
// A water heater class is defined here
Public class Water Heater
{
Public void (Object sender, EventArgs e)
{
// Add water to the water tank to be added
}
}
Public class User
{
Public User ()
{
// Instantiate the Water Tank Class
Tank 1 = new tank ();
// Instantiate the water heater class
Sprinkler 1 = new sprinkler ();
// Register the empty water tank event with the water heater
Tank 1. Empty tank + = new EventHandler (water dispenser 1. Add water );
}
Private Water Heater 1;
Private Water Tank 1;
}
The most classic interpretation of delegate C:
Example: this. Activated + = new EventHandler (Form1_Activated );
This is the principle of delegation.
This. Activated = after dinner;
Form1_Activated = give me a shout
This sentence means to put these two things together, that is, to call me after dinner. I entrusted you with a meal and shouted at me. In this way, I don't need to wait a while to see if you have finished eating and have already commissioned you.