C # delegated Learning

Source: Internet
Author: User

Although the. NET industry has been around for almost two years, the foundation is not very good. Today I have read the entrusting party's knowledge and recorded it.

1. Delegate

In general, a delegate is a class that defines the type of the method so that the method can be passed as a parameter of another method. This way, the method is dynamically assigned to the parameter, it can avoid using the If-Else (Switch) statement in a large number in the program, and make the program more scalable.
Therefore, after the delegate is introduced, the programmer can encapsulate the method reference in the delegate object, and then pass the delegate object to the method to be referenced.
The method for calling a delegate is the same as that for calling a method. The Code is as follows:

 

A. Code:

WriteTextBox (button1_Click (checkBox1.Checked = writeTextBox = (checkBox2.Checked = writeTextBox = data = (I =; I <data. length; I ++ DateTime now = (now. addSeconds ()> WriteTextBox1 (WriteTextBox2 (Form1.cs

 

B .:

 

2. Delegated chain

In fact, the delegated chain is a delegate, but it only contains multiple delegates. After reading the following code, you can understand it.

A. Code:

Main (DelegateTest dtstatic = DelegateTest dtinstance = DelegateTest (DelegateTest delegatechain ==++ = Console. writeLine (Environment. newLine + dtstatic () + Environment. newLine); Console. writeLine (dtstatic. invoke () + Environment. newLine); Console. writeLine (Environment. newLine + Test (delegatechain); Exception (chain = StringBuilder returnstring = Delegate [] delegatearray = (DelegateTest t returnstring. append (t () + (Exception e)Program. cs

 

B .:

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.