C# question

Started by
3 comments, last by oatz01 16 years, 6 months ago
In C++ you can have function pointers as arguements using a declaration like
void func(void (*func2)());
Is there similar functionality in C#?
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)
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
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.
Perfect. Thanks guys.

This topic is closed to new replies.

Advertisement