Jump to content
  • Advertisement
Sign in to follow this  
oatz01

C# question

This topic is 3942 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!