Are there any disadvantages of using delegates ?

J

James

Hello,

I am new to C# programming and I am using delegates for the event-
driven application and found them very useful.
Are there any applications where delegates may not be useful ? Can
anyone please let me know what are the disadvantages of using
delegates.

Thanks,
James.
 
C

Cowboy \(Gregory A. Beamer\)

A delegate is simply an anonymous function pointer. It allows you to more
freely configure a call to any method that bears the same signature of the
delegate.

I don't know of any application where a delegate is not useful, except
perhaps trying to delegate across a web service. :)

There are situations, however, you should not use a delegate. For exmample,
calling a private method in the same class. That would simply be a method
call.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
J

James

Well, more generally: when calling _any_ method when the method name is  
actually always known and accessible at compile time, even if the method  
is protected, public, or internal.  It would be pretty pointless to usea  
delegate in that situation.

But, for situations where a delegate is actually called for, the overhead 
is minimal and the design benefit significant.  So, as far as that goes, I  
agree that there's no specific reason to shun the use of a delegate.

Pete

Thanks a lot

James
 
P

Peter Morris

I don't recall the details, but I once had a problem with something like
this

foreach (Thing currentThing in things)
TasksToDo.Add(
new TaskToDo(
currentThing.Name,
delegate()
{
currentThing.DoSomethingOrOther();
}
);

Where currentThing would always be the last value for all, because the same
delegate instance was used for each item in the list. If I recall the
solution was....

foreach (Thing currentThing in things)
TasksToDo.Add(
new TaskToDo(
currentThing.Name,
delegate()
{
Thing theThing = currentThing;
theThing.DoSomethingOrOther();
}
);

Not a disadvantage, more of a gotcha. All vague now though :)
 
I

Israel

when calling _any_ method when the method name is  
actually always known and accessible at compile time, even if the method  
is protected, public, or internal.  It would be pretty pointless to usea  
delegate in that situation.

I've done that as a quick and easy way to call a method asynchronously
when I don't want the caller to have to wait rather than launching my
own thread to handle the request. Might as well just use the thread
pool.
 
I

Ignacio Machin ( .NET/ C# MVP )

There are situations, however, you should not use a delegate. For exmample,
calling a private method in the same class. That would simply be a method
call.

Not really you can decide which method to cal @ runtime. In this case
you need a delegate.
 
I

Ignacio Machin ( .NET/ C# MVP )

Where currentThing would always be the last value for all, because the same
delegate instance was used for each item in the list. If I recall the
solution was....

That's the expected behavior, weir maybe but documented. IIRC Jon
Skeet wrote an article about this. Do a search by Jon Skeet and Closure
 
P

Peter Morris

That's the expected behavior, weir maybe but documented. IIRC Jon
Skeet wrote an article about this. Do a search by Jon Skeet and Closure

I know, I was just making the original poster aware of it :)
 
M

Mike Schilling

James said:
Hello,

I am new to C# programming and I am using delegates for the event-
driven application and found them very useful.
Are there any applications where delegates may not be useful ? Can
anyone please let me know what are the disadvantages of using
delegates.


If you're coming from Java, then a delegate will be a good thing to
use whenever you would have used an anonymous class in Java.
 
M

Mike Schilling

Peter said:
Not really. Only for anonymous classes implementing an interface
with one method would a delegate be an appropriate replacement. In
fact, one way to think of a delegate is exactly that: as a
single-method interface.

True; in the case analogous to an interface with multiple methods
you'd use multiple delegates, one per.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top