Anyone please explain with example what is callback function delegate ? when to use ?...
Thanks in Advance
Anyone please explain with example what is callback function delegate ? when to use ?...
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajeesh MenothPosted Aug 23, 2016, 8:32 AM
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)
Vinay SinghPosted Aug 23, 2016, 8:17 AM
Joginder BangerPosted Aug 23, 2016, 4:10 AM
A callback is basically a delegate passed into a procedure which that procedure will "call back" at some appropriate point. For example, in asynchronous calls such as WebRequest.BeginGetResponse or a WCF BeginXxx operation, you'd pass an AsyncCallback. The worker will "call back" whatever method you pass in as the AsyncCallback, in this case when it's finished to let you know that it's finished and to obtain the result.
An event handler could be considered another example. For example, when you attach a handler to a Click event, the button will "call back" to that handler when the click occurs
Source : Statckoverflow.com