Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

When do we use Delegates in C#.Net?

Posted on 2010-09-06
4
Medium Priority
?
840 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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