Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Why we should use delegate?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Swapnil - 26 Feb 2008 04:22 GMT
As we know we can call function from the class by using object of the class
where that function is present.
Then what is new in delegate it also calls the function but the difference
is that we are providing wrapper around the function call. iI know it is
typesafe. Explain with some strong cocept(reason).
Then why we should use Delegate?
e.g.
  Suppose i have function in Myclass class

class Myclass{ void Add (int a){}}

To call this function i will use object of Myclass.

Myclass objMyclass=new Myclass();
objMyclass.Add(4);

like this i can call the function.

But by using delegate
delegate void Delegatename(int x);     //definition of dlegate
Delegatename objDelegate=new Delegatename(objMyclass.Add(4));

so by using delegate we r giving overheads then why we should use delegate?
Mufaka - 26 Feb 2008 04:51 GMT
You wouldn't use it like this in practice. Delegates provide a level of
indirection where your instance can be invoked by a class that has no
knowledge of your method beyond it being an instance of a delegate type.

> As we know we can call function from the class by using object of the class
> where that function is present.
[quoted text clipped - 19 lines]
>
> so by using delegate we r giving overheads then why we should use delegate?
jehugaleahsa@gmail.com - 26 Feb 2008 05:13 GMT
> As we know we can call function from the class by using object of the class
> where that function is present.
[quoted text clipped - 19 lines]
>
> so by using delegate we r giving overheads then why we should use delegate?

In my experience, delegates come in handy when you are working with
collections. Outside of collections, delegates can be used, as said,
to add a level of indirection. Additionally, multicast delegates allow
multiple methods to be attached at once - most people see this through
events.

Keeping that in mind, delegates should NEVER be used where a good
class heirarchy and/or interfaces and/or polymorphic behavior makes
sense. Like I said, my primary use for delegates have been to work on
a collection, where the delegates play the role of black boxes, the
changing part of the code. If you think about that, delegates provide
a way of working on data where the data may not adhere to a specific
interface. The behavior might change but the data will not.
Polymorphic behavior can work across a heirarchy, but not within the
same class.

So, in summary, delegates find there greatest use, for me, in
1) Events
2) Generic Algorithms and Data Structures
3) The rare need to change a class' behavior at runtime.

Fortunately, most delegates are built-in and you don't have much to
do. If delegates don't make sense, you're trying to hard to use them.
Keep them in your toolset and an opportunity will show itself.

~Travis Parks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.