Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a "typedef to a function pointer" in C#?

Tags:

c#

I'm converting code from C++ to C#. I have this line:

typedef bool (*proc)(int*, ...);

Can I do that in C#?

like image 620
KairisCharm Avatar asked Aug 28 '14 20:08

KairisCharm


People also ask

What is typedef in function pointer?

A typedef, or a function-type alias, helps to define pointers to executable code within memory. Simply put, a typedef can be used as a pointer that references a function.

Can you typedef a function?

You can declare any type with typedef , including pointer, function, and array types. You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration.


1 Answers

Short answer: Yes.

Generally:
(untested... just an outline)

{
    bool AFunction(ref int x, params object[] list)
    {
        /* Some Body */
    }

    public delegate bool Proc(ref int x, params object[] list);  // Declare the type of the "function pointer" (in C terms)

    public Proc my_proc;  // Actually make a reference to a function.

    my_proc = AFunction;         // Assign my_proc to reference your function.
    my_proc(ref index, a, b, c); // Actually call it.
}
like image 94
abelenky Avatar answered Sep 16 '22 21:09

abelenky