Delegates and Callbacks

Is the term callback appropriate in the context of delegates: "delegating the delegate that he performs to another delegate to complete some task"?

Example: ( Based on my understanding, I implemented a callback, correct me if it is wrong )

namespace Test { public delegate string CallbackDemo(string str); class Program { static void Main(string[] args) { CallbackDemo handler = new CallbackDemo(StrAnother); string substr = Strfunc(handler); Console.WriteLine(substr); Console.ReadKey(true); } static string Strfunc(CallbackDemo callback) { return callback("Hello World"); } static string StrAnother(string str) { return str.Substring(1, 3).ToString(); } } } 

Indicate examples if necessary.

+6
c # callback
source share
4 answers

Your example is a good start, but it is wrong. You do not create a new delegate in the method that was used when declaring the event in the class. See this modified example of your code:

 namespace Test { //In this case, this delegate declaration is like specifying a specific kind of function that must be used with events. public delegate string CallbackDemo(string str); class Program { public static event CallbackDemo OnFoobared; static void Main(string[] args) { //this means that StrAnother is "subscribing" to the event, in other words it gets called when the event is fired OnFoobared += StrAnother; string substr = Strfunc(); Console.WriteLine(substr); Console.ReadKey(true); //this is the other use of delegates, in this case they are being used as an "anonymous function". //This one takes no parameters and returns void, and it equivalent to the function declaration //'void myMethod() { Console.WriteLine("another use of a delegate"); }' Action myCode = delegate { Console.WriteLine("another use of a delegate"); }; myCode(); Console.ReadKey(true); //the previous 4 lines are equivalent to the following however this is generally what you should use if you can //its called a lambda expression but it basically a way to toss arbitrary code around //read more at http://www.developer.com/net/csharp/article.php/3598381/The-New-Lambda-Expressions-Feature-in-C-30.htm or //http://stackoverflow.com/questions/167343/c-lambda-expression-why-should-i-use-this Action myCode2 = () => Console.WriteLine("a lambda expression"); myCode2(); Console.ReadKey(true); } static string Strfunc() { return OnFoobared("a use of a delegate (with an event)"); } static string StrAnother(string str) { return str.Substring(1, 3).ToString(); } } } 

I just scratched the surface here; search stack overflow for "delegate C #" and "lambda expression C #" for more!

+11
source share

A callback is basically a delegate passed to a procedure that this procedure will โ€œcall backโ€ at some suitable point. For example, in asynchronous calls, such as WebRequest.BeginGetResponse or WCF BeginXxx operations, you must pass AsyncCallback. The worker will "redirect" any method that you pass as an AsyncCallback, in which case it will finish to inform you that it is finished and get the result.

An event handler can be considered another example. For example, when you attach a handler to a Click event, the button will โ€œreturnโ€ to that handler when you click.

+3
source share

A callback is what the delegate does when it is called. For example, when using an asynchronous template using delegates, you would do something like this:

 public static void Main(string[] args) { Socket s = new Socket(...); byte[] buffer = new byte[10]; s.BeginReceive(buffer, 0, 10, SocketFlags.None, new AsyncCallback(OnMessageReceived), buffer); Console.ReadKey(); } public static void OnMessageReceived(IAsyncResult result) { // ... } 

OnMessageReceived is a callback, code executed by calling a delegate. See this Wikipedia article for more information, or google some more examples.

+3
source share

This will be voted precisely for the reason that it is right. C # does not implement delegates, what it implements is call forwarding. This misuse of nomenclature is probably the biggest problem for C # in this regard.

The following is an ACM article - the first description of what will later be called a delegate. Basically, a delegate is what seems like an instance of an object (how it actually is implemented doesn't matter). This means that you can call methods, access properties, etc. From the delegate.

http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.html

Which C # implement callbacks or call forwarding (it all depends on how you use them). These are not delegates. To be a delegate, you can access the object as if it were the object itself.

When the pen delegates the message about drawing to the prototype pen, he says: โ€œI donโ€™t know how to handle the message about the call. Id like you to answer it if you can, but if you have additional questions, what is the value of my variable x or you need to do something, you must come back to me and ask. " If the message is additionally delegated, all questions about the values โ€‹โ€‹of variables or queries for answering messages are all displayed on the object that delegated the message in the first place. - Henry Lieberman

So how is he messed up? Honestly, I do not know. I know that I used delegates (over 16 years) long before C #, and what C # implements is not a delegate.

You can find a really good explanation here.

http://www.saturnflyer.com/blog/jim/2012/07/06/the-gang-of-four-is-wrong-and-you-dont-understand-delegation/

Real delegation is more than making callbacks or forwarding calls. The real delegate allows me to call any method for this object, get and / or set public properties, etc. - everything, as if it were the object itself. It is much more powerful and actually easier to use than C # "delegation".

OP asked:

Is the term callback appropriate in the context of delegates: "delegating the delegate that he performs to another delegate to complete some task"?

The answer is yes. A callback in the context of delegates can be used to complete a task. For example, you have a class that receives data from a weather site. Since it is not deterministic, the implementation of a callback when the data has been received (and possibly analyzed) will suffice.

As to why the delegation was damaged, I have no idea. I look forward to C # performing TRUE delegation.

+1
source share

All Articles