C # graphic tutorials Study Notes - Principal

First, the commission outlined
delegate and classes, user-defined type, is a reference type. But class represents the collection of data and methods, and delegate holds one or more methods, and a set of predefined operations.
Delegate may be used by the following steps:
(1) a delegate type declaration. Method statement and proxy statement looks similar, but not implemented block.
(2) use of the delegate type to declare a delegate variable.
(3) create an object delegate type, assign it to a variable commission. The new delegate object includes a reference point to a method, this method and the first step in defining the signature and return the same type.
(4) the option to add other methods to a delegate object. These methods must be defined in the first step with the same type of delegate signature and return type.
(5) You can call the delegate method calls like the same code. When the delegate is invoked, it contains each method will be executed.
Notes:
(1) commission, the list of methods called call list.
(2) The method of holding delegate may be from any class or structure, may be an instance method may be a static method, as long as they have the same type of the delegate signature and return type can.
(3) It will be called once if a method appears more than once in the call list, when the delegate is invoked, each time it encounters the method in the list.
(4) The delegate is constant. The delegate object that can not be changed after being created.

 

Second, the delegate of type declarations and object creation
1. delegate type declaration

grammar:

2. Create a delegate object
to create a delegate object will allocate memory for the commission, will be put into commission the first method invocation list.
Create one:

Creating Method II (shorthand syntax):

Icon:

 

Third, commissioned operation
due after the delegate object is created is a constant immutable, it is a combination of commission, the delegate added method, removed from the delegate methods are essentially created a new commission.
1. Combination commissioned
a combination of commission: essentially create a new commission, which is connected to the call list call list as a copy of two delegates operands.
Example:

Icon:

2. To add a delegate methods
C # supports the use of the + = operator to call the delegate list of methods to increase.
Example:

3. removed from the delegate method
supports C # - = operator is removed from the list call the delegate method.
Example:

Note:
(1) If there are multiple instances in the call list, - = operator to start the search from the end of the list, and removes the first instance of a matching method.
(2) attempt to delete the delegate method that does not exist no effect.
(3) attempts to call an empty delegate throws an exception. By entrusting null and compare commissioned to determine whether the list is empty. If the call list is empty, then the commission is null.
4. Call the delegate
can be as simple as calling a method call delegate. Parameters will call delegate list for each method call.
Example:

Icon:

 

Fourth, the commission's arguments and return values issues
1. Call the parameters with reference to a delegate
if the delegate has a reference parameter, the parameter values will vary according to the return value of a call list or more methods, the last value is to call the delegate list All methods in turn act on the results of parameters.
2. Call entrusted with the return value
if the delegate has a return value and call list more than one method, the return value is the value of the commission calls a method returns a list of the last call, the other return value is ignored.
3. Using an anonymous method to the delegate assignment
example:

 

Guess you like

Origin www.cnblogs.com/wujuntian/p/10990334.html