Sign in to follow this  

C# question

This topic is 3729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In C++ you can have function pointers as arguements using a declaration like
void func(void (*func2)());
Is there similar functionality in C#?

Share this post


Link to post
Share on other sites
Delegates.

delegate void IntMethod(int value);

// ...

void JustPrintIt(int value)
{
Console.WriteLine(value);
}

void CallFunction(IntMethod method, int value)
{
method(value);
}

// ...

CallFunction(JustPrintIt, value);


(untested)

Share this post


Link to post
Share on other sites
Yes, they are called delegates and look like:


access delegate return_type DelgateName (function parameters);


So, in your case:

public delegate void Func2 ();

public void Func (Func2 func2)
{
}


Skizz

Share this post


Link to post
Share on other sites
Yes, delegates are the way to this in C# however you should be aware that delegates are more than just function pointers.
A few extras to note:
Delegates are classes; they have methods and properties
Delegates can point to multiple functions (multicast delegates)
Delegates have built-in support for asynchronous calls.
When trying to implement properties of delegates, consider using events. Events are 'ecapsulated' delegate properties.

Share this post


Link to post
Share on other sites

This topic is 3729 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this