Phani Katakam 26-Apr-Apr-15 23:17Come on Guys! All of you successfully complicated the DELEGATES!I will try to leave a hint here: i understood delegates once I realized jquery ajax calls in Javascript. For ex: ajax.send(url, data, successcallback, failcallback) is the signature of the function. As you know, it sends data to the server URL, as a response, It might be 200OK or some other error. In case of any such event(success/fail), you want to execute a function. So, this acts like a placeholder of a function, to be able to mention in either success or failure.That placeholder may not be very generic - it might accept a set of parameters and may/may not return value. That declaration of such Placeholder, if done in C# IS CALLED DELEGATE!
As javascript functions not strict with number of arguments, you would just see them as GENERIC placeholders.but C# has some STRICT declarations. That boils down to DELEGATE declarations!!Hope it helps! Idanbismut 31-Oct-09 5:1931-Oct-09 5:19First i wanted to say thank you for a great article.Examples listed here are easy enough to understand, unlike other articles posted here that are to 'lengthy' or contain rather 'complex' examples.One note though, I think I found some typo/error in your article.You have defined:public delegate void testDelegate(int a);public event testDelegate MyEvent;and in example written:Myform.MyEvent += new testEvent(MyMethod);while as far as i understood (hoping i'm correct), it should be as follows:Myform.MyEvent += new testDelegate(MyMethod). Rocky Moore 18-Aug-03 4:2618-Aug-03 4:26I think Delegates and Events are one of the areas a lot of programmers end up stumbling through (I have a few stubbed toes on this one;) ). That may be why we see so many articles posted here.Would be nice thought to give a little more information the delegate use/creation than the reference to MSDN. In that light, two articles I found usefull are the two part series by Jeffrey Richter in MSDN Magazine (I beleive April 2001 and June 2001).One of the greatest problems I had while trying to wrap my brain around delegates was the declaration of a delegate.First, it usually said to declare it outside of your class which seemed strange to me.
C# provides function types to fit a variety of scenarios, called delegates, events, and lambda functions. In this course, Joe Marini explores these key elements, starting with delegates, which allow developers to easily separate functional pieces of a program. The problem Invoking event handlers in C# has always been a bit of a pain, because an event with no subscribers is usually represented as a null reference. This leads to code like this: It's important to use the handler local variable, as if instead you access the field twice, it's possible that the last.
It does not seem to have a problem declared inside or outside of your class except for the class name prefixing required to access it.Finally, the hard part was the mystery of the declaration. It seemed like a header file definition back in my C days. I had to search through a ton of articles and messages posted to newsgroups before I noticed that it simply generates a class derived from System.MultiDelegate. That one little tidbit, made things clear up for me. I was no longer confused by the definition of the delegate since I could see clearly what it generated in the ILDASM.The only other problem I have left is the usage of the 'event' keyword. I can see where it is building me a add/remove for the delegate, but exactly what benifit is that? I can see in the ILDASM that it is tagged as an event rather than just a class instance, but I do not see the help here.
The delegate can handle the add/remove itself with the +=/-= and the code seems to function with or without the 'event' keyword. What is it that I am missing here?Rocky Moore. With or without the event keyword, the logic works just fine. I.e you are able to add event handlers with += and remove them using -=.But how many times have you tried to add an event handler using = instead of +=? (I've done that many times Inadvertently)? Without using the event keyword, you'll overwrite the delegate, and only your event handler that was registered last would be called whenever the event occurs.this one clear advantage of using the event keyword is that it ensures that you only add or remove event handling functions to delegates and not overwrite them. With or without the event keyword, the logic works just fine.
I.e you are able to add event handlers with += and remove them using -=.But how many times have you tried to add an event handler using = instead of +=? (I've done that many times Inadvertently)? Without using the event keyword, you'll overwrite the delegate, and only your event handler that was registered last would be called whenever the event occurs.this one clear advantage of using the event keyword is that it ensures that you only add or remove event handling functions to delegates and not overwrite them.