24. Delegate (1)

Source: Internet
Author: User

1. Simple delegate (tom eats watermelon ......)

 

Code

 Namespace _ 24. Delegate _ 1 _
{

Delegate void Eat (string food );
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Static void xwEat (string food)
{
MessageBox. Show ("Xiao Wang eat" + food );
}
Private void Form1_Load (object sender, EventArgs e)
{

Eat xw = new Eat (xwEat );
Xw ("watermelon ");
// XwEat ("watermelon ");
}
}

}

2. Continue the above expansion

Code

 Namespace _ 24. Delegate _ 1 _
{

Delegate void Eat (string food );
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Static void xwEat (string food)
{
MessageBox. Show ("Xiao Wang eat" + food );
}
Static void xlEat (string food)
{
MessageBox. Show ("Xiao Li eat" + food );
}
Static void xzEat (string food)
{
MessageBox. Show ("Xiao Zhao eat" + food );
}
Private void Form1_Load (object sender, EventArgs e)
{

Eat xw = new Eat (xwEat );
Eat xl = new Eat (xlEat );
Eat xz = new Eat (xzEat );
Xw ("watermelon ");
Xl ("watermelon ");
Xz ("watermelon ");
// XwEat ("watermelon ");
}
}

}

3. Continue the above expansion

Code

 Namespace _ 24. Delegate _ 1 _
{

Delegate void Eat (string food );
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Static void xwEat (string food)
{
MessageBox. Show ("Xiao Wang eat" + food );
}
Static void xlEat (string food)
{
MessageBox. Show ("Xiao Li eat" + food );
}
Static void xzEat (string food)
{
MessageBox. Show ("Xiao Zhao eat" + food );
}
Private void Form1_Load (object sender, EventArgs e)
{

Eat xw = new Eat (xwEat );
Eat xl = new Eat (xlEat );
Eat xz = new Eat (xzEat );
Eat EatChain;
EatChain = xw + xl + xz;
EatChain ("watermelon ");

}
}

}

4. Continue the above expansion

Code

 Namespace _ 24. Delegate _ 1 _
{

Delegate void Eat (string food );
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Static void xwEat (string food)
{
MessageBox. Show ("Xiao Wang eat" + food );
}
Static void xlEat (string food)
{
MessageBox. Show ("Xiao Li eat" + food );
}
Static void xzEat (string food)
{
MessageBox. Show ("Xiao Zhao eat" + food );
}
Private void Form1_Load (object sender, EventArgs e)
{

Eat xw = new Eat (xwEat );
Eat xl = new Eat (xlEat );
Eat xz = new Eat (xzEat );
Eat EatChain;
MessageBox. Show ("three-person meeting ");
EatChain = xw + xl + xz;
EatChain ("watermelon ");
MessageBox. Show ("John goes out to answer the phone ");
EatChain-= xw;
EatChain ("grape ");
MessageBox. Show ("Tom comes back to eat oranges with everyone ");
EatChain + = xw;
EatChain ("orange ");

}
}

}

5. Continue the above expansion

Code

 Namespace _ 24. Delegate _ 1 _
{

Delegate void Eat (string food );
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e)
{
Eat eatChain = null;
EatChain + = delegate (string food)
{
MessageBox. Show ("Xiao Wang eat" + food );
};
EatChain + = delegate (string food)
{
MessageBox. Show ("Xiao Zhao eat" + food );
};
EatChain ("watermelon ");

}
}

}

 

 

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.