C # Delegate and C's function pointers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.