Solved

When do we use Delegates in C#.Net?

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now