Function pointer in C #

zhaozj2021-02-16  56

The function pointer also called the pointer to the function, which is introduced to improve the versatility of the function. The compare traditional method is to increase the parameters in the function. By adding the parameters to determine what function to use, such a disadvantage is the flexibility, in order to support a number of methods to write a lot of branch judgment statements in the function. For example, you have to calculate the square of a function value (such as SiN X, Log10 x), and you can write this function according to the traditional method:

Public Double Fuction (Double Param, Int FunctionSelection {if (FunctionSelection = 0) Return Math.Pow (Math.SIN (PARAM), 2); if (functionSelection = 1) Return Math.Pow (Math.log10 (PARAM), 2); .............. }

Obviously, this method is very much, causing a function of a function. Therefore, the idea of ​​function pointers is important. Everyone may be familiar with the DDP function pointer type in C , then what is the function pointer in the C # language of the fully object?

In C # we use the Delegate keyword to implement function pointers. There is a classic example in ".NET Framework Essential":

using System; namespace testDelegate {public class testDelegate {// 1 defines a callback function pointer delegate void MsgHandler (string strMsg);.. // 2 defines a callback function void OnMsg (string strMsg) {Console.WriteLine (strMsg);..} Public static void main () {TestDelegate t = new testdelegate (); // 3. Connection function pointer object f to t.onmsg. msghandler f = new msghandler (t.onmsg); // 4. call pointer's callback function f ("Hello, Delegate!");}}}

The above example comment describes the work to be done by the implementation of the function. We found that the parameters in the Delegate Void MsgHandler (String strmsg) are string type, indicating that the function that can point to only one string parameter, and MSGHandler F = New MSGHandler (T.Onmsg); is transferred directly to F in this function. This example is too simple, but the usage of the function pointer has been described. We may wish to solve the problem of squares that calculate different functional values.

First define a function delegate (pointer):

DELEGATE DOUBLE DOUBLEHANDLER (Double D_Parm);

This is equivalent to the type of function pointer in C .

The second step defines the callback function is free because we can use the math.sin function and the math.log10 function, but we must define a function to seek square:

Public Static Double MathMethod (Double Parm1) {Return Math.pow (Parm1, 2);

This function is more simpler than the function of the previous plus parameters, but it is more versatile.

The following is to establish an object to the previously defined function pointer type (ie the function delegate) and initialize it to its function:

DoubleHandler DoubleMethod = New Doublehandler (Math.sin);

Everyone has seen, this pointer points to the Math.sin function, which is equivalent to completing "assignment" (initialization) for function pointer types in C . You can also change the function objects of the function pointer by adding DoubleMethod = New Doublehandler (Math.log10); reinitialize. Finally, it is the square of the calculation function value:

Double D = MathMethod (DoubleMethod (.5);

The following is a complete program, debugging under Visual Studio.net 2003:

Using system;

Namespace Testdelegate {public class testdelegate {delegate void msghandler (String strmsg); DELEGATE DOUBLE DOUBLEHANDLER (Double D_Parm);

Void onmsg (string strmsg) {console.writeline ("The result is: / t {0}", strmsg);

Public Static Double MathMethod (Double Parm1) {Return Math.pow (Parm1, 2);

Public static void main () {testdelegate t = new testdelegate (); msghandler f = new msghandler (t.onmsg);

DoubleHandler [] doubleMethod = {new doublehandler (math.sin), new doublehandler (math.log10), new doublehandler (math.sqrt)}} {f (MathMeth (Handle (.5)). TOSTRING ()); // Call the object f of MSGHandler to display the calculation result}}}}}}}}}}}}}}}}

The above example can explain the problem, consciously use the C # delegate programming implementation function pointer, can effectively simplify the code, improve the versatility of the function. (Finish)

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

New Post(0)