Skip to content

Instantly share code, notes, and snippets.

@ohlawdie
Last active February 11, 2020 22:53
Show Gist options
  • Save ohlawdie/7edb1d1baf2ab23dbda78a86b17ed737 to your computer and use it in GitHub Desktop.
Save ohlawdie/7edb1d1baf2ab23dbda78a86b17ed737 to your computer and use it in GitHub Desktop.
Callback #delegate #callback
If you're used to function pointers in C, a delegate is basically a pair of pointers rolled into one:
A pointer to an object (optional)
A pointer to a method of that object
That means a single delegate passes all the information needed to locate a function in your program, whether it's a static method or associated with an object.
You define them like this in C#:
public delegate void FooCallbackType( int a, int b, int c );
When you want to use them, you make delegate out of the function you want to call:
class CMyClass
{
public void FunctionToCall( int a, int b, int c )
{
// This is the callback
}
public void Foo()
{
FooCallbackType myDelegate = new FooCallbackType(
this.FunctionToCall );
// Now you can pass that to the function
// that needs to call you back.
}
}
///are delegates references
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment