Introduction:
A small example, hope can help the vast number of Chevalier to understand delegate.
Example of a function pointer in C:
1#include <stdio.h>2 3 //Subtraction Operations4 intMinus (intAintb) {5 returnAb;6 }7 8 //addition Operation9 intSumintAintb) {Ten returnA +b; One } A - //This counting function is used to do the calculation between A and B, as for addition or subtraction, which is determined by the 1th parameter of the function. - voidCounting (int(*p) (int,int) ,intAintb) { the intresult =P (A, b); -printf"The result is:%d\n", result); - } - + intMain () - { + //perform addition operations ACounting (SUM,6,4); at - //perform the subtraction operation -Counting (minus,6,4); - - return 0; -}
This code is quoted from: Http://www.cnblogs.com/mjios/archive/2013/03/19/2967037.html.
This page has a detailed tutorial on pointers to functions, and if you are not very familiar with C Chevalier, you can go to the exact point.
Write your own C # code:
1 classupstatic2 {3 Public Delegate intCalculateintAintb);4 Public intAddintAintb)5 {6 returnA +b;7 }8 Public intMinus (intAintb)9 {Ten returnAb; One } A Public intMultiplyintAintb) - { - returnAb; the } - Public intCountintAintb, calculate c) - { - intresult =C (A, b); + returnresult; - } + } A at class Program - { - - Static voidMain (string[] args) - { - varTest =Newupstatic (); in intp = Test.count ( -, -, Test.add); - intQ = Test.count ( -, -, Test.minus); to intR = Test.count ( -, -, test.multiply); + Console.WriteLine (p); - Console.WriteLine (q); the Console.WriteLine (r); * Console.read (); $ }Panax Notoginseng}
Enlightenment:
Castles in the castle are always unsettling, and the base is high.
To lay a good foundation, we can stand steady.
C # Delegate and C's function pointers