6

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

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

Can I do that in C#?

1
  • 8
    Take a look at delegates Commented Aug 28, 2014 at 20:16

2 Answers 2

3

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. } 
Sign up to request clarification or add additional context in comments.

2 Comments

proc is a Type. proc = AFunction; wouldn't compile. (I can see this "untested... just an outline", but test it.)
Ah - The params keyword was the part I was unaware of. Thank you so much.
2

Delegates are not strictly equivalent, but used for similar purposes.

Sample:

public delegate void DoSth(string message); void foo(string msg) { // ... } void bar() { DoSth delg = foo; delg("tttttt"); } 

If you are trying to invoke code from native C++ libraries using P/Invoke, you will need to take a look at GetDelegateForFunctionPointer and GetFunctionPointerForDelegate Method pair of functions.

2 Comments

I am not the downvoter, however, an answer that starts with "Read up on..." is generally not a real answer.
You also probably meant to do DoSth delg = foo;.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.