Are delegates and callbacks the same or similar?

Are delegates the same as callbacks? Or are they connected in some way?

+44
callback delegates
Nov 14 '08 at 17:41
source share
4 answers

“Callback” is a term that refers to a coding design pattern available in any language that has pointers to functions, or pointers to analog functions (which is a kind of delegate)

In this template, you pass a pointer to a function of another function, so that inside the called function, it can “call back” the function that you passed to it. Thus, you can control a large chunk of the internal behavior of the method outside the method by passing pointers to different "callback" functions each time you call it ... An example of a callback is choosing a sorting algorithm that should be passed a pointer to a function that will be “compare” any arbitrary pair of objects in a sorted list to determine what happens before another. With one call to the sorting method, you can pass a callback function that compares by the name of the object, and another time passes a function that compares by the weight of the object or something else ...

The delegate, otoh, is specific. By a Net type that acts as a signature-specific container for a function pointer ... In managed .Net code, delegates are the only way to create and use a function pointer. Therefore, in .Net, you actually pass a delegate to make a callback ... But delegates can be used in scripts other than callbacks. (in particular, delegates can also be used to implement multithreading from a .Net thread pool)

Callbacks are also used to implement the observer pattern (implemented in .Net using events, which themselves are a special type of delegate)

+30
Nov 14 '08 at 17:58
source share

(I assume you are talking about .NET here. If not, please clarify.)

Delegates are an idiomatic way to implement callbacks in .NET, but you don't need it. For example, you can use the interface. (In particular, you could have one callback with one method to raise an error, and one with success. Of course, you could take two delegates instead ...)

There are many possibilities in .NET for use outside of callbacks. It depends on what you consider a callback, but GUI event handlers, streaming starts, filters and forecasts (and much more!) In LINQ to Objects all use delegates.

+14
Nov 14 '08 at 17:45
source share

in general, a delegate is an object used to access an external method of an object that owns this method, and a callback is a variable containing a delegate

in C #, the terms are used interchangeably

+4
Nov 14 '08 at 17:44
source share

They are connected to each other. A delegate is a description of what the callback function looks like:

delegate void MyDelegate(string Text); 

Then you may have a function that can take a callback as a parameter.

 //This will result in a MessageBox with "Lalalala" MyFunctionThatGetsTheCallbackFunctionRef(MyCallBackFunc); void MyFunctionThatGetsTheCallbackFunctionRef(MyDelegate TheFunction){ TheFunction("Lalalala"); } void MyCallBackFunc(string Text){ //my callback MessageBox.Show(Text); } 
+4
Nov 14 '08 at 17:49
source share



All Articles