For example, in a company (scenario), you are the boss and have two employees, Xiao Zhang and Xiao Wang.
You ordered Mr. Zhang to play the game. If Mr. Zhang played the game, he deducted 500 yuan from Mr. Zhang.
This is the delegate in reality.
In fact, in writing a program, the programmer is the boss, and Xiao Zhang and Xiao Wang are two objects. Playing a game is a method. Another game event is played by John. Mr. Smith is the object of event processing. He is responsible for deducting Mr. Zhang's money by 500.
Therefore, delegation has the following elements:
1. The object that inspires the event is Xiao Zhang.
2. The object for processing the object event is John.
3. Define the delegate, that is, let Mr. Smith monitor Mr. Zhang.
If all three elements are met, you can write a complete event.
The following example shows that the application is successfully edited and run in the Vs. net2003 C # console:
Using system;
Namespace csharpconsole
{
Public class scenario
{
[Stathread]
Public static void main (string [] ARGs)
{
Console. writeline ("the scenario has started ....");
// Generate Tom
Wang W = new Wang ();
// Generate a small account
John Z = new John ();
// Specify monitoring
Z. playgame + = new playgamehandler (W. Deduction );
// Start playing the game
Z. Play the game ();
Console. writeline ("End of scenario ...");
Console. Readline ();
}
}
// The person responsible for fee deduction
Public class Wang
{
Public Wang ()
{
Console. writeline ("generate Tom ...");
}
Public void deduction (Object sender, eventargs E)
{
Console. writeline ("Mr. Wang: Good boy, dare to play games during work hours ...");
Console. writeline ("JOHN: look at how much your kid has ...");
John f = (John) sender;
Console. writeline ("John's money:" + F. Qian. tostring ());
Console. writeline ("start to deduct money ......");
System. Threading. thread. Sleep (500 );
F. Money = f. Money-500;
Console. writeline ("the button is finished... Now, James is still there:" + F. Qian. tostring ());
}
}
// If the game is played, an event is triggered.
Public class Xiao Zhang
{
// Define an event first, which means "Xiao Zhang" is playing the game.
Public event playgamehandler playgame;
// Save the small money variable
Private int m_money;
Public Xiaozhang ()
{
Console. writeline ("generating images ....");
M_money = 1000; // constructor, which initializes the small Zhang's money.
}
Public int money // This property can be used to operate on the small Zhang's money.
{
Get
{
Return m_money;
}
Set
{
M_money = value;
}
}
Public void ()
{
Console. writeline ("John started playing the game .....");
Console. writeline ("Xiao Zhang: CS fun, hahaha! I play .....");
System. Threading. thread. Sleep (500 );
System. eventargs E = new eventargs ();
Onplaygame (E );
}
Protected virtual void onplaygame (eventargs E)
{
If (playgame! = NULL)
{
Playgame (this, e );
}
}
}
// Define the delegate Handler
Public Delegate void playgamehandler (Object sender, system. eventargs E );
}
Where:
In the money deduction event:
John f = (John) sender;
This Code allows Mr. Smith to deduct his money. When Mr. Smith deducts money, he must perform operations on his object instance.
If you read the code carefully, you will understand the following method:
Public void deduction (Object sender, eventargs E)
There is an object sender object in the method parameter. This object indicates the object that inspires the event. In this program, this sender is actually a small image, but the object type passed is object, therefore, the object type conversion statement must be used to convert the sender from the object to a small one:
John f = (John) sender;
System. Threading. thread. Sleep (500 );
This code pauses the program for half a second. The unit of 500 is millisecond, and the unit of 500 is half a second.
In the small Zhang class, there is a line of code
Protected virtual void onplaygame (eventargs E)
It indicates whether the playgame event has been registered by another object. Because the playgame event of the small Zhang class may not have other types of notifications to be processed, the Code is as follows:
If (playgame! = NULL)
If no object needs to be detected, an event notification should be sent; otherwise, an object needs to process the event, and an event notification should be sent for the object to process.