A wonderful article about. Net Delegation

Source: Internet
Author: User

Many people ask about delegation and event handling in. net. Here is a simple example:

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 );

}

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.