Excellent article. I'm at a point where I perfectly understand how to use delegates/events/anon methods and lambdas. However what seems to torture me is not understanding exactly WHY use delegates instead of regular method calls.
I understand how using a delegate can allow you to substitute code during run time in your class on the fly, simply by reassigning what method your delegate is referencing. So basically if I understand correctly, a delegate or a callback, allows me to write a class which can communicate back to the caller, like the main() method which calls a procedure in my class for example, and that class in turn, can call back some method in my Program class, so basically a delegate allows communication between objects. Is that correct and is that the main purpose of delegates? I also know that .NET library makes you use delegates a lot. Could you clarify and perhaps give a bit more explanation about exactly when I want/must use delegates? Thanks, I think I just had a revelation just writing this and I think I'm starting to understand the point. Thanks!
Wow, this is a very extensive question : when and why use delegates ? The truth is, there is no single answer. I was hoping that my article would give a hint when you can use a delegate. But in fact, there are many situations when you can/want/must use delegates. They are a technique that is available in the C#, VB.Net and perhaps others languages, just like for instance interfaces or abstract classes, and can be used and misused. I think the more you use them, even just for fun in private and/or test projects, the more you will get to know them. And know know knowing them is love love loving them, as the song goes ! Thanks for writing !
I'm glad the example helped you to understand delegates better. But please remember that this is only 1 way of using delegates. Events are another, lambda expressions yet another. So keep on studying !
I'm sorry you find the example "not godd" (only joking ) But can you be a bit more specific ? Is it too complicated ? Too long ? Not clear enough ? I suggest you copy the code and debug it step-by-step. I think you'll it find not that difficult at all ! Thank you for your vote