473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Are there any disadvantages of using delegates ?

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.
Oct 3 '08 #1
12 6704
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! |
********************************************
"James" <bo*************@gmail.comwrote in message
news:3a**********************************@m3g2000h sc.googlegroups.com...
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.
Oct 3 '08 #2
On Fri, 03 Oct 2008 09:14:07 -0700, Cowboy (Gregory A. Beamer)
<No************@comcast.netnospammwrote:
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.
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 use a
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
Oct 3 '08 #3
On Oct 3, 11:53*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 03 Oct 2008 09:14:07 -0700, Cowboy (Gregory A. Beamer) *

<NoSpamMgbwo...@comcast.netnospammwrote:
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.

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
Oct 3 '08 #4
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 :-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 3 '08 #5
On Oct 3, 12:53*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 03 Oct 2008 09:14:07 -0700, Cowboy (Gregory A. Beamer) *
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.
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.


Oct 3 '08 #6
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.
Oct 3 '08 #7
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
Oct 3 '08 #8
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 :-)


--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Oct 3 '08 #9
James wrote:
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.
Oct 4 '08 #10
On Fri, 03 Oct 2008 18:43:58 -0700, Mike Schilling <ap@newsgroup.nospam>
wrote:
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.
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.

In Java, an anonymous class can implement an interface with multiple
members, or can even inherit from other classes. A delegate is not of
much use in those situations.

Pete
Oct 4 '08 #11
Peter Duniho wrote:
On Fri, 03 Oct 2008 18:43:58 -0700, Mike Schilling
<ap@newsgroup.nospamwrote:
>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.

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.

Oct 4 '08 #12
On Fri, 03 Oct 2008 22:21:16 -0700, Mike Schilling <ap@newsgroup.nospam>
wrote:
True; in the case analogous to an interface with multiple methods
you'd use multiple delegates, one per.
Personally, in the case analogous to an interface with multiple methods,
I'd use an interface.

To each his own, I suppose.
Oct 4 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Billy Jacobs | last post by:
I forgot to capitalize the line causing the error. It is: printer.PrintCallback(10)
12
by: VM | last post by:
Hi, In my Win app, I'm using delegates because the process that'll be running is time-consuming and it requires parameters. The only problem I'm having is that the lines following the BeginInvoke...
2
by: Max Adams | last post by:
Question about using delegates with a string paramater to return a ListViewItem object... All, I have a thread and I want this thread to post messages to the main GUI thread using a delegate....
3
by: Oscar Thornell | last post by:
Hi, I am thinking about doing all my logging asynchronously using a delegate. The main resaon for this would be performance and responsiveness of the application (an ASP.NET app). //Exampel...
1
by: keith.walter | last post by:
My first asp.net app is almost "done" and I am stuck. Here is my situation: I have a "mother" page add_customer.aspx and a"child" user control add_group.ascx. On the mother page is an "add...
3
by: Rich | last post by:
Hello, The following Delegates example works with Option Strict Off, but if I change it to Option Strict On then VB.Net complains about the line dgY = System.Delegate.Combine(dgI, dgA) Is...
3
by: dast | last post by:
Hi, I'm using a delegate to edit some controls on formA from another thread on formB. This works nicely the first time I run formA. But if I close formA and reopen it I get errors. When...
2
by: =?Utf-8?B?LnBhdWwu?= | last post by:
i'm using this method with subs + it works well, but with functions, they don't return a value. what am i doing wrong? Private Delegate Function listview_items_countCallback() As Integer ...
15
by: Pixel.to.life | last post by:
Dear All, Here is a problem I am facing (it might be too simple, but then I admit I am not a Guru:-) I have a main thread, in managed C++, that deals with displaying a form and some controls....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.