C # language series lecture (9) delegation

xiaoxiao2021-03-06  79

Delegation delegate is a new data type introduced by C #, which is very similar to a function pointer in C / C , often used to call the unbound when compiling. Unlike functional pointers, delegate fully implement the object-oriented object in C #, which can reference the static method, or the instance method, and the function pointer can only reference the static method. The delegation in C # is also a type of security. As an object-oriented data type, the delegated use is divided into three steps: delegation declaration, delegate instantiation and delegation calls. Delegation declaration is a data type that defines a method body (static method or instance method) that encapsulates a specific parameter type and the return value type. Look at the following example: delegate int compute (int tent); you can see, delegate type Compute Two elements that contain methods: parameter types and return value types. Delegation types and methods only satisfy the following two conditions can we be compatible: 1. The number of parameters is the same, and their types are also the same as sequential; The return value is the same. The delegation type is the same as that they declare the same type (the same name). The delegation instance is equal to the method of which they are bound to the same method, or the same method, according to the method chain of the same order, and their own type is compatible (equally satisfied with two conditions), do not have to force The same name. Delegation instantiation is the process of binding the delegated type to a particular method, and the instantiation of other objects, requires a new statement, just accepts the method name compatible with the delegate as a parameter of the new statement. If it is an instance method, an instance object and method must be provided simultaneously in the middle of the intermediate plus point number. After delegate instantiation, you can delegate calls like a modified method. Here is a complete example, more typically three steps that are usually used: use system; delegate int compute (int tent, int right); // delegated type declaration class a {public int mul (int Num1, int Num2) {return Num1 * Num2;}} class test {public static int add (int Num1, int Num2) {RETURN NUM1 NUM2;} static void main () {compute c1 = new compute (add); // Static method Instantiation a a = new a (); compute c2 = new compute (a.mul); // instance method instantiation INT R1 = C1 (2, 3); // Delegation call INT R2 = C2 (2, 3); // Delegate call console.writeline (r1); console.writeLine (R2);} The delegation of the delegation means that a delegate type can bind multiple modread methods at the same time. Due to the multiple methods of binding, C # specifies the return type of the combination delegate must be Void. Of course, the parameters of these methods must also be compatible with the parameter type of the combination. The delegation combination uses " " or " =" to combine two delegated types into a new combination delegation type; use "-" or "- =" to remove the delegation of a method that has been bound to a combination delegation type A combination delegation instance of instance or binding multiple methods. It should be noted that the delegation type of the participating operation must be the same when making a delegate combination and removal operation - Note that "the same" is not "equal".

转载请注明原文地址:https://www.9cbs.com/read-121283.html

New Post(0)