Preliminary probe. Delegate type with .NET events

xiaoxiao2021-03-05  37

1. Learn how DelegatedElegate can think is a function of a function, an instance representative of a delegate type can represent a method, in actual use we can call to this method without knowing the method name. As mentioned earlier, too much may make everyone confused, or you can't touch your mind, I will give an example to specify this type of usage:

The code of the entire routine is a console project.

// Code starts USING System;

namespace ConsoleApp1 {public delegate string FunctionPointer (string strName); class Class1 {[STAThread] static void Main (string [] args) {// // TODO: Add code to start the application System.Console.WriteLine here ( " hello World ");! FunctionPointer fpa = new FunctionPointer (aFunction); FunctionPointer fpb = new FunctionPointer (bFunction); InvokeMethod (fpa); InvokeMethod (fpb); System.Console.ReadLine (); //} public static string aFunction ( string sNameA) {return "aFunction say i'm" sNameA;} public static string bFunction (string sNameB) {return "bFunction say i'm" sNameB;} public static void InvokeMethod (FunctionPointer fp) {System.Console. WriteLine (FP ("storm"));}}} // code end

Step 1: To use the delegate type, you should first create a Delegate model. The so-called delegate model is actually the signature of the method that delegate can call, what is the method's signature? The signature of the method can be considered to be the parameter type, quantity, return value type of the method.

The defined situation is as follows: Access the Decorator Delegate Return Value Type Name (Parameter List);

Note: The return value type should be the return value type of the method you define the method prepared by the call. The parameter list should be the same as the parameter list of the methods you are in the method you want to use (refer to the quantity, type, the same order, name Of course, it doesn't matter) an actual example is the Public delegate String FunctionPointer (String Strname) of the above program;

Step 2: Define the method of delegate ready to call, of course, the method has the same return value and parameter table as the delegate you just declared; for example, the above program: public static string Afunction (String snamea) {return "Afunction Say I'm " snamea;} public static string bFunction (String snameb) {returnction" BFunction Say I'm " snameb;} Step 3: Define the delegate type handler, where you can define how to call your delegate type In actual use, you can dynamically determine information such as incoming parameters here. Public static void invokeMethod (FP ("storm97");} ("storm97"));}

Step 4: Establish a Delegate instance, incoming way to call the method name, to use the delegate type, you must establish his instance, an instance represents a reference to a method (you can say this)

Delegate instance definition form: XXX (name to find a Delegate type) instance name (assuming is instance a) = new xxx ([Method Name "(assuming is method a)]); meaning instance variable A Pointing the program code segment of method A;

Static void main (String [] args) {// // Todo: Add code here to launch the application system.console.writeline ("Hello World!"); // Establish the first Delegate instance FPA, this Example representative method AFUNCTION; functionPointer FPA = new functionPointer (AFUNCTION); // Establishing the first Delegate instance FPA, the instance represents method bFunction; functionPointer fpb = new functionPointer (bFunction); ... //} below You can run the entire program. Such as: static void main (string [] args) {// ... // call method AFUNCTION InvokeMethod (FPA); // Call method BFunction InvokeMethod (fpb); //}

Output Result: Afunction Say I'm Storm97 BFunction Say I'm Storm97

Of course, from this example, you can't see what is the advantage of delegate, because this program is too simple, maybe someone will say that the way is going to be so troublesome, I am directly calling, why should I call around a large circle? In fact, This method will bring us great flexibility in practical applications, and you will slowly experience in the actual use process. :)

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

New Post(0)