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

Why use a delegate

Following on from a previous thread in which types were discussed. I
was interested to read more about these differeent types. I started by
trying to learn about the delegate type, of which I had never seen
before.

I have so far managed to find out that a Delegate type is simmilar to a
function pointer, but the main difference is that it is type safe. I
haven't come accross the term function pointer before, but from what I
can gather the the delegate merely lets you call a static method, or
call an instance method. To facilitate this it takes either a static
method, or an object and instantiated method as it's arguments.

I can't see what it's actual use is?

Why not just call the method directly?

Thanks,

Gary-

Dec 7 '06 #1
3 1534
Hello,
>Why not just call the method directly?
The typical reason for that is that from a particular piece of code, it is
not known what that method is going to be. A traditional example for this
is a simple sorting algorithm - that sorting algorithm needs to compare
values, and so it uses a function pointer or a delegate to do the
comparing, which makes the sorting algorithm very universal in its
application.

This kind of implementation can be found in .NET's List<T>.Sort() method,
as one of its overloads takes a delegate of type Comparison<Tas a
parameter. This MSDN page has an example of using that method:

http://msdn2.microsoft.com/en-us/library/w56d4y5z.aspx

Another thing worth mentioning is that the whole event system is based on
delegates. So every time you write some code in an event handler on a
form, you're basically creating a delegate method that is being used by
the Windows Forms library (for example) - obviously the Windows Forms
developers didn't know you were going to write that particular piece of
code, so they used delegates as an "extensibility" feature in their library.
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 7 '06 #2

It's good to think of Delegates as basically how Events work. Of
course they can be used for callbacks and other things (another poster
mentioned sorting an array) but for those types of callback Interfaces
are much cleaner and self documenting.

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On 7 Dec 2006 03:56:04 -0800, ga********@myway.com wrote:
>Following on from a previous thread in which types were discussed. I
was interested to read more about these differeent types. I started by
trying to learn about the delegate type, of which I had never seen
before.

I have so far managed to find out that a Delegate type is simmilar to a
function pointer, but the main difference is that it is type safe. I
haven't come accross the term function pointer before, but from what I
can gather the the delegate merely lets you call a static method, or
call an instance method. To facilitate this it takes either a static
method, or an object and instantiated method as it's arguments.

I can't see what it's actual use is?

Why not just call the method directly?

Thanks,

Gary-
Dec 7 '06 #3
Samuel R. Neff <sa********@nomail.comwrote:
It's good to think of Delegates as basically how Events work. Of
course they can be used for callbacks and other things (another poster
mentioned sorting an array) but for those types of callback Interfaces
are much cleaner and self documenting.
I disagree with that - particularly when you consider anonymous
methods. Something like List<T>.FindAll works very neatly with
anonymous methods, keeping the predicate cleanly with the code using
it. (Not so good if you're using the same kind of predicate in multiple
places, but that's a different matter.)

To do the same thing with an interface requires an extra class to be
used just for the sake of matching things (or bolting it onto an
existing class, which is usually a bit of a hack) *and* it doesn't
provide the access to other data (local variables etc) that anonymous
methods do.

They're equivalent in terms of what you *can* do after some effort, but
given the choice between a single-method interface and a delegate, I'd
usually say the delegate is an easier thing to work with.

(The recent RC1 release of Groovy allows a closure to automatically
implement a single-method interface. I've already found that
considerably neater than "properly" implementing interfaces.)

--
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
Dec 7 '06 #4

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

Similar topics

3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
3
by: Jeff S | last post by:
Please consider this sample code: It registers a delegate with an event. p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged); Now the following code removes the delegate:...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.