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 .: