Detailed Explanation of EventHandler Example in C
- 2021-12-12 09:32:10
- OfStack
No more nonsense, the specific details are as follows:
// This defines the 1 Water tank type
public class Water tank
{
// This is the water discharge operation of the water tank
public void Discharge water () { }
// This is the property of the water tank
public double Volume ;
// This is an event where the water tank is empty
public event EventHandler Empty water tank ;
}
// This defines the 1 Water feeder class
public class Water feeder
{
public void Add water (Object sender, EventArgs e)
{
// Add water to the water tank that needs to be added
}
}
public class User
{
public User()
{
// Instantiate the water tank class
Water tank 1 = new Water tank ();
// Instantiate water feeder class
Water feeder 1 = new Water feeder ();
// Water heater registered empty water tank event
Water tank 1. Empty water tank += new EventHandler( Water feeder 1. Add water );
}
private Water feeder Water feeder 1;
private Water tank Water tank 1;
}
--------------------------------------------------------------------------------------------
The most classic explanation of C # delegation:
Examples:
this.Activated += new EventHandler(Form1_Activated);
This is the principle of a delegate.
this.Activated= You finish your meal ;
Form1_Activated= Call me 1 Sound
The meaning of this sentence is to put these two things in one, which means to call me once you have finished eating. I entrust you to call me once you have finished your meal. In this way, I don't have to come to see if you have finished eating. I have entrusted you.