1. What is commissioned by the 1.1 Official explanation
A delegate is a type that defines the method signature. When instantiating a delegate, You can associate its instance with any method with a compatible signature. You can call a method by entrusting an instance.
1.2 understanding
A delegate is a class that executes methods (functions.
An event is a special delegate.
Ii. How to delegate 2.1 delegate
Public Delegate int testdelegate (int x, int y );
2.2 Action
Action is a generic delegate without return values.
Action indicates a delegate with no parameter and no return value.
Action <int, string> indicates that the input parameter int exists, and the string value does not return a delegate.
2.3 func
Func is a generic delegate with a returned value.
Func <int> indicates that there is no parameter, and the return value is the delegate of Int.
Func <object, String, int> indicates that the input parameter is an object, and the string return value is an int delegate.
2.4 Predicate
Predicate is a generic delegate that returns the bool type.
Predicate <int> indicates that the input parameter is int and the bool delegate is returned.
Differences between 2.5 and 4
Delegate has at least 0 parameters and up to 32 parameters. You can specify no return value or the return value type.
The action must have at least one parameter, at most four parameters, and no return value,
Func must have at least 0 parameters and at most 4 parameters, which are returned Based on the return value of the generic type. There must be a return value, not void
Predicate has at least one parameter and at most one parameter. The return value is fixed to bool.
Iii. How to Use delegate 3.1 labmda expressions
Testdelegate D2 = (string name) => {console. writeline ("Hello, {0 }! ", Name );};
D2 ("Terry ");
3.2 anonymous method
Delegate void testdelegate (string myname );
Testdelegate D2 = delegate (string name)
{
Console. writeline ("Hello, {0 }! ", Name );
};
D2 ("test ");
3.3 function declaration
Private void delegatemethod (string name)
{
Console. writeline ("Hello, {0 }! ", Name );
}
Testdelegate D2 = new testdelegate (delegatemethod );
D2 ("test ");
Iv. Features of delegated use
Delegation is similar to C ++ function pointers, but they are type-safe.
The delegate allows passing methods as parameters.
A delegate can be used to define a callback method.
The delegate can be linked together. For example, multiple methods can be called for an event.
The method does not have to match the delegate signature completely.
V. Delegated Use Cases
Delegation is generally used in observer mode (Observer mode ).
The observer design mode is used to define a one-to-many dependency between objects, so that when the state of an object changes, other objects dependent on it will be automatically notified and updated.
The observer mode mainly includes the following two types of objects:
Monitored object: usually contains the content that other objects are interested in.
Monitored by: When something in an object occurs, the builder is notified, and the builder takes corresponding actions.
For example, when youProgramWhen processing a large volume of data, you need to display a progress bar on the program interface for friendly prompts. At this time, you can achieve this through delegation.
Example:
Public Delegate void delegatemethod (INT position, int maxvalue );
Public class testdelegate
{
Public delegatemethod ondelegate;
Public void dodelegatemethod ()
{
Int maxvalue = 100;
For (INT I = 0; I <maxvalue; I ++)
{
If (this. ondelegate! = NULL)
{
This. ondelegate (I, maxvalue );
}
}
}
}
Testdelegate test = new testdelegate ();
This. textbox1.text = "";
This. progressbar1.value = 0;
Test. ondelegate = new delegatemethod (delegate (int I, int maxvalue)
{
This. textbox1.text + = I. tostring () + environment. newline;
This. progressbar1.maximum = maxvalue;
This. progressbar1.value ++;
});
Test. dodelegatemethod ();
6. How to clear delegation
1. Declare the delegate method in the class and remove the delegate reference cyclically.
The method is as follows:
Public class testdelegate
{
Public delegatemethod ondelegate;
Public void cleardelegate ()
{
While (this. ondelegate! = NULL)
{
This. ondelegate-= This. ondelegate;
}
}
}
2. If the method for clearing the delegate is not stated in the class, we can use getinvocationlist to query the delegate reference and then remove it.
The method is as follows:
Testdelegate test = new testdelegate ();
If (test. ondelegate! = NULL)
{
System. Delegate [] dels = test. ondelegate. getinvocationlist ();
For (INT I = 0; I <dels. length; I ++)
{
Test. ondelegate-= dels [I] As delegatemethod;
}
}
Insert
Delegate I used in practice
Statement:
Private delegate void fnshowitem ();
Private dictionary <string, fnshowitem> callshowitem;
Instance:
Callshowitem. Add ("Add new employee information", showitemfornewemployee );
Callshowitem. Add ("batch change shift", showitemforshift );
Callshowitem. Add ("batch change supervisor", showitemforsuper );
Callshowitem. Add ("batch change senior supervisor", showitemforsrsuper );
Callshowitem. Add ("batch change leader", showitemforleader );
Instance:
Private void showitemfornewemployee ()
{
Hideall ();
This. cbbelonglocation. Visible = true;
This. comboline. Visible = true;
// This. comboprocess. Visible = true;
// This. comboquarters. Visible = true;
// This. combocraftwork. Visible = true;
This.txt belongleader. Visible = true;
This.txt belongsuper. Visible = true;
// This. btn_sleader.visible = true;
// This. btn_ssuper.visible = true;
// This.txt quarterskind. Visible = true;
This. cbwipinfo. Visible = true;
This. btn_newadd.visible = true;
Settiptext ("default", color. Maroon );
Setsrsupervisor ("where Mark = 'A '");
}
Call:
This. callshowitem ["changing shifts in batches"] ();