Monday, March 12, 2012

Why use delegates?

Hi,

I'm pretty new at this so please be gentle. This might be obvious to many of you out there but I just can't understand why delegates should be used. From the books I've been reading it looks like it's just a fancy way of using "if" statements.

Can anyone explain or point me to a good online tutorial on Delegates and Events?

Thanks,

RobThey're great for event driven programming.

If you have ever used C/C++, then you'll recognize them as alternatives to function pointers.
Thanks for the reply.

I have never used C/C++, and that's part of my problem. Every Tutorial/Book that has information on Delegates assumes the reader knows what a function pointer is...

I would appreciate it if someone could answer the following questions,

1) Why would I use a delegate?
2) Can anyone provide a real-world example of when I should use a delegate?
3) An example of a task that can't be done without the use of a delegate?
4) The relationship between Delegates and Events?

I know I might be asking for too much, but you could also point me out to a good article on delegates that doesn't assume knowledge of C/C++ pointers.

Thanks,

Rob
"1) Why would I use a delegate? "

Callbacks.

2) Can anyone provide a real-world example of when I should use a delegate?

For example I have a threading that can be launched by a variety of different objects and it takes in a callback function to notify the calling object when certain stages are complete etc. The callback is a function of the object and has it's own implementation, but the thread just knows to call it at certain points. Progress bars etc can make good use of callback functions.

3) An example of a task that can't be done without the use of a delegate?

Event driven programming in .NET. Delagates are used to wire everything up.

4) The relationship between Delegates and Events?

Events use delagates to hookup actions. For instance, the onClick event for a button is hooked up to the button via a delagate of the onclick method you add.

If you want to read a very good description of delagates, I recommend picking up the 'Inside C#' book from Microsoft Press. It has a chapter on it that explains it quite well I think.
Thanks, I'll take a look at the book.

Hi rblino,

Please share your knowledge / finding on the use of delegates with me also. I am also in the same situation and unable to understand the exact use of delegates. I have used c/c++ but never been into the loops of function pointers and all that kind of stuff so do not much about it.

I think you have asked quite a good questions on this and will really helpful to the others also and may be save lot of time of others doing the R&D which you have already done.

If possible, please provide practical examples of it, if you could use that somewhere in your project and it has really provided a robustness to your application. Looking for the answers of the questions which you have asked in the original post.

Thanks in advance.

Regards,

onlibedev

0 comments:

Post a Comment