Solved

When do we use Delegates in C#.Net?

Posted on 2010-09-06
4
822 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
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wav problem 4 25
.net and XML report from SCCM 5 20
Finally almost got it 5 36
Create a WebAPI using a generic return parameter 9 24
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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