Solved

When do we use Delegates in C#.Net?

Posted on 2010-09-06
4
832 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 150 total points
ID: 33613124
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 150 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 200 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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 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