Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are delegates and callbacks the same or similar?

People also ask

Is C# delegate a callback?

Understanding delegates in C#Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.

What's the difference between callback and function?

The main difference between a normal function and a callback function can be summarized as follows: A normal function is called directly, while a callback function is initially only defined. The function is only called and executed once a specific event has occurred.

What do you mean by callbacks?

1 : a return call. 2a : recall sense 5. b : a recall of an employee to work after a layoff. c : a second or additional audition for a theatrical part.

Why are callbacks called callbacks?

Simply put: A callback is a function that is to be executed after another function has finished executing — hence the name 'call back'.


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)


(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.


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);
}

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