473,385 Members | 1,542 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,385 software developers and data experts.

The practical use of delegates

My question is regarding the use of delegates in C#. I see how .Net uses
delegates to wire event handlers to events. It’s an object created by a
single line of code by the system and that makes perfect sense to me. I
understand that there is a lot of code underneath that the system has created
that makes it all work, thereby making it pretty efficient for the programmer.

Outside of the use of delegates to wire event handlers, you can create your
own delegates to make a call to a method, which according to some, makes
delegates perfect for implementing callbacks. My question is this:

In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance methods).
Are delegates good all the time, some of the time, or when? Am I missing
something? Why are so many so high on using delegates as much as they can?
Is there a security issue that makes delegates better than a direct call?

I just don’t have enough experience yet to figure this one out yet. Any
help will be appreciated. Thanks in advance for your help.

--
Scott
Sep 24 '07 #1
6 2617
Scott,

Well, the callback is a perfect example. You might know the method that
needs to be called at a certain point in the procedure, but that would tie
that code to that specific procedure, and not make the code very flexible.

Great examples of this are callbacks in Sockets when data is read, data
base connections when commands are executed, etc, etc. Delegates are a
great way to indicate the action that should be taken when an operation is
complete.

Granted, you could use interface implementations (create a specialized
interface which has a method with the signature you need for the callback)
but that can be a little bit of a hassle if you want to keep your code more
clean/concise (as the implementation would typically be in another class,
but then you have to have that other class call back into the calling class
to do the work). This is one of the things I didn't like in the early days
of Java (we are talking 10 years ago).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"OldCaDog" <sb*******@community.nospamwrote in message
news:62**********************************@microsof t.com...
My question is regarding the use of delegates in C#. I see how .Net uses
delegates to wire event handlers to events. It's an object created by a
single line of code by the system and that makes perfect sense to me. I
understand that there is a lot of code underneath that the system has
created
that makes it all work, thereby making it pretty efficient for the
programmer.

Outside of the use of delegates to wire event handlers, you can create
your
own delegates to make a call to a method, which according to some, makes
delegates perfect for implementing callbacks. My question is this:

In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance
methods).
Are delegates good all the time, some of the time, or when? Am I missing
something? Why are so many so high on using delegates as much as they
can?
Is there a security issue that makes delegates better than a direct call?

I just don't have enough experience yet to figure this one out yet. Any
help will be appreciated. Thanks in advance for your help.

--
Scott

Sep 24 '07 #2

One way to consider the use of delegates is like this
"Delegates are kinda Microsoft's "built in" Observer Pattern".

See
http://www.dofactory.com/Patterns/PatternObserver.aspx

Instead of doing the Observer pattern , with an interface like IObserver,
you can use delegates to handle this.
So anywhere that the Observer pattern would make sense, you can use
delegates.


"OldCaDog" <sb*******@community.nospamwrote in message
news:62**********************************@microsof t.com...
My question is regarding the use of delegates in C#. I see how .Net uses
delegates to wire event handlers to events. It's an object created by a
single line of code by the system and that makes perfect sense to me. I
understand that there is a lot of code underneath that the system has
created
that makes it all work, thereby making it pretty efficient for the
programmer.

Outside of the use of delegates to wire event handlers, you can create
your
own delegates to make a call to a method, which according to some, makes
delegates perfect for implementing callbacks. My question is this:

In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance
methods).
Are delegates good all the time, some of the time, or when? Am I missing
something? Why are so many so high on using delegates as much as they
can?
Is there a security issue that makes delegates better than a direct call?

I just don't have enough experience yet to figure this one out yet. Any
help will be appreciated. Thanks in advance for your help.

--
Scott

Sep 24 '07 #3
Hi,

"OldCaDog" <sb*******@community.nospamwrote in message
news:62**********************************@microsof t.com...
My question is regarding the use of delegates in C#. I see how .Net uses
delegates to wire event handlers to events. It's an object created by a
single line of code by the system and that makes perfect sense to me. I
understand that there is a lot of code underneath that the system has
created
that makes it all work, thereby making it pretty efficient for the
programmer.

Outside of the use of delegates to wire event handlers, you can create
your
own delegates to make a call to a method, which according to some, makes
delegates perfect for implementing callbacks. My question is this:

In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance
methods).
You are right, but what if you do not know the actual method you will need
to call?
A callback is an excellent example of this.

In this case you need a feature that let you dynamically select which method
to call. This feature are the delegates.
Sep 24 '07 #4
OldCaDog <sb*******@community.nospamwrote:

<snip>
In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance methods).
And when you know exactly what to call at the point you want to make
the call, you might as well call the method directly.

Delegates are specifically for situations where you want the action to
take place at one point of the code, but you want to be able to control
what happens from another point of code.

In C# 1, delegates are relatively rarely used outside event handlers
and callbacks.

In C# 2 there are more uses (e.g. List.ConvertAll etc) and various
language enhancements make them easier to use.

In C# 3 there's *far* more scope to use delegates due to LINQ etc - and
lambda expressions make them really, really concise.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 24 '07 #5
Thank you all for taking the time to comment, I really appreciate it! You've
all given me much more to consider and investigate, thanks again.
--
Scott
"Jon Skeet [C# MVP]" wrote:
OldCaDog <sb*******@community.nospamwrote:

<snip>
In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance methods).

And when you know exactly what to call at the point you want to make
the call, you might as well call the method directly.

Delegates are specifically for situations where you want the action to
take place at one point of the code, but you want to be able to control
what happens from another point of code.

In C# 1, delegates are relatively rarely used outside event handlers
and callbacks.

In C# 2 there are more uses (e.g. List.ConvertAll etc) and various
language enhancements make them easier to use.

In C# 3 there's *far* more scope to use delegates due to LINQ etc - and
lambda expressions make them really, really concise.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 24 '07 #6

"OldCaDog" <sb*******@community.nospamwrote in message
news:62**********************************@microsof t.com...
In a lot situations, I can create less code by calling that function or
method directly rather than using delegates (for static or instance
methods).
Are delegates good all the time, some of the time, or when?
This thread is mostly about when to use delegates.
Let me turn it on its head, becuase you seem to have heard
that delegates are good, and that you should use them.

But I'd like to stress this, that has not yet to mentioned yet in this
thread.
But you seem to have figured it out. :)

- If you know the method (on a known instance) of a specific class to
call...
- You should call it directly!

By using a delegate to call a known method,
you are just not only engaging in total overkill, it is bad code!
As your code would suggest the call is dynamic and could change,
when it is not.

String.Format("Only exception is if you really hate
your {0} maintainers {1}, "future", ", that might be yourself 2 years
later");

..NET Is Not Java :)
- Michael Starberg
Sep 26 '07 #7

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

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.