Are delegates and callbacks the same or similar?

.NetCallbackDelegates

.Net Problem Overview


Are delegates the same thing as callbacks? Or are they related somehow?

.Net Solutions


Solution 1 - .Net

A "callback" is a term that refers to a coding design pattern, available in any language that has function pointers, or an analogue to function pointers (which is kinda what a delegate is)

In this pattern, you pass a pointer to a function to another function, so that within the called function, it can "call back" the function you passed to it. In this way you can control a large chunk of the internal behavior of a method from outside the method, by passing pointers to different "callback" function each time you call it... An example of a callback is when you have a sorting algorithm that has to be passed a pointer to a function that will "compare" any arbitrary pair of objects in the list to be sorted, to determine which goes before the other. On one call to the sort method, you might pass a callback function that compares by object name, and another time pass a function that compares by object weight, or whatever...

A Delegate, otoh, is a specific .Net type that acts as a signature-specific container for a function pointer... In .Net managed code, delegates are the only way to create and use a function pointer. So in .Net to do a callback, you are in fact passing a delegate... But delegates can be used in other scenarios besides callbacks. (specifically, delegates can be also used to implement multi-threading from the .Net thread pool)

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

Solution 2 - .Net

(I assume you're talking about .NET here. If not, please elaborate.)

Delegates are the idiomatic way of implementing callbacks in .NET - but you don't have to. You could use an interface, for example. (In particular you could then have one callback with one method to call on error, and one on success. Of course, you could take two delegates instead...)

There are plenty of uses for delegates beyond callbacks in .NET - it depends on exactly what you deem to be a callback, but GUI event handlers, thread-starters, filters and projections (and more!) in LINQ to Objects all use delegates.

Solution 3 - .Net

They are hand in hand related. A delegate is the description on how the callback function looks like:

delegate void MyDelegate(string Text);

Then you can have a function that can take in the 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);
}

Solution 4 - .Net

generically, a delegate is an object used to access a method external to the object owning the method, while a callback is a variable that holds a delegate

in C#, the terms are used interchangeably

Solution 5 - .Net

The service class has a variable function called a delegate. The calling class has a preferred function that it wants run called a callback.

The calling class sets the delegate to it's callback.

The difference between a delegate and a callback is the perspective: The service class calls the delegate that is set to the calling classes callback.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionASDFQWERTYView Question on Stackoverflow
Solution 1 - .NetCharles BretanaView Answer on Stackoverflow
Solution 2 - .NetJon SkeetView Answer on Stackoverflow
Solution 3 - .NetWolf5View Answer on Stackoverflow
Solution 4 - .NetSteven A. LoweView Answer on Stackoverflow
Solution 5 - .NetWatachiaietoView Answer on Stackoverflow