Looking for work? Check out our jobs area.
*/

Other Views

corner
*/

CSHARP FAQ - Implementing Function Pointers in C#

How do I implement function pointers in C#?

Delegates provide the functionality of function pointers in C#. Here is an example that illustrates both:

Function pointer example:
typedef int (*FUNC_PTR)(void *, void *);
int Compare(void* a, void* b) { ... }
int CallFunction(void* a, void*b)
{
    FUNC_PTR myFunc = Compare;
    
    for (i = left+1; i <= right; i++)
        if (myFunc(v[i], v[left]) < 0)
    
}


Equivalent delegate example:
public delegate int FUNC(object x, object y);
public int Compare(object x, object y) { ... }
public void sort()
{
    CMP_Func myFuncObj = new FUNC(Compare);
    ...
    if (myFuncObj(x, y) < 0)
        System.Console.WriteLine("Object X is less than Object Y");
    else if (myFuncObj(x, y) = 0)
        System.Console.WriteLine("Object X equals Object Y");
    else
        System.Console.WriteLine("Object X is greater than Object Y");
}


Index


corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings