?
Solved

When do we use Delegates in C#.Net?

Posted on 2010-09-06
4
Medium Priority
?
836 Views
Last Modified: 2013-11-08

I am trying to understand what scenarios we use delegates? I was able to understand an example of a delegate but I dont get why do we have to use delegates.

Thanks for any clarifications or good article to understand why do we use delegates
0
Comment
Question by:ipjyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 600 total points
ID: 33613124
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 600 total points
ID: 33613127
Delegates are basically method pointers, used predominantly with event handlers. There are also anonymous delegates (used by LINQ for example).

More detailed info here:

    http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx
0
 
LVL 12

Accepted Solution

by:
w00te earned 800 total points
ID: 33613282
Hey :)
Delegates can be used for a  number of things as they're pretty much the C# version of C++ funciton pointers.  Alot of time when you're working with events in the language you're actually using delegates in the background, the language just kind of abstracts that away from you.  The link below shows event handling using delegates directly rather than C# event structures.
Anyway, if you want a real scenario where they can be used, think about this:
We have a winforms program in C#.  One of its operations is to do a massive backup of files and directories.  When the backup starts, it is spawned in another thread so that the UI doesn't block up.
The problem now is that that new thread has no access to the form, so if the thread wishes to update a progress bar on the form to show the progression of the backup, it cant do it.  We can solve this problem by providing a delegate to a function on the form.  The delegate can take the required parameters regarding percentage, and call the appropriate form function to update the progress bar.
The thread can call the delegate on the form, which essentially makes the form update the progress bar itself even though the thread was the original requestor.  Basically, instead of trying to get direct access to the form's elements, the thread uses the delegate to "ask" the form to update the progress bar itself via the delegate.
http://www.codeproject.com/KB/cs/csevents01.aspx 
Hope that clears things up a little bit!
-w00te
0
 

Author Comment

by:ipjyo
ID: 33613352
Thanks for the responses and for the example scenario.




0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question