25. Delegate (2)

Source: Internet
Author: User

1. In the previous section, the delegate is declared using static methods. In this section, the delegate is declared using dynamic methods to make it more flexible.

Code

 Namespace _ 25. Delegate _ 2 _
{

Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e)
{
Man xw = new Man ("John ");
Man xl = new Man ("Xiao Li ");
Man xz = new Man ("Xiao Zhao ");
EatDelegate a = new EatDelegate (xw. eat );
EatDelegate s = new EatDelegate (xl. eat );
EatDelegate d = new EatDelegate (xz. eat );
EatDelegate eatChain = null;
MessageBox. Show ("three-person open Part ");
EatChain = a + s + d;
EatChain ("watermelon ");
}
}
Delegate void EatDelegate (string food );
Class Man
{
Private string name;
Public Man (string name)
{
This. name = name;
}
Public void eat (string food)
{
MessageBox. Show (name + "eat" + food );
}
}
}

2. Next, the above contains multiple parameters.

 

Code

 Namespace _ 25. Delegate _ 2 _
{

Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
Man xw = new Man ("John ");
Man xl = new Man ("Xiao Li ");
Man xz = new Man ("Xiao Zhao ");
EatDelegate a = new EatDelegate (xw. eat );
EatDelegate s = new EatDelegate (xl. eat );
EatDelegate d = new EatDelegate (xz. eat );
EatDelegate eatChain = null;
MessageBox. Show ("three-person open Part ");
EatTogether ("watermelon", a, s, d );
MessageBox. Show ("xw goes out ");
EatTogether ("grape", s, d );
MessageBox. Show ("xw comes here ");
EatTogether ("orange", a, s, d );
EatTogether (null, null );
}
Static void eatTogether (string food, params EatDelegate [] values)
{
If (values = null)
{
MessageBox. Show ("Party is over! ");
}
Else
{
EatDelegate eatChain = null;
Foreach (EatDelegate ed in values)
EatChain + = ed;
EatChain (food );
MessageBox. Show ("");
}
}
}
Delegate void EatDelegate (string food );
Class Man
{
Private string name;
Public Man (string name)
{
This. name = name;
}
Public void eat (string food)
{
MessageBox. Show (name + "eat" + food );
}
}
}

 

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.