 |
| C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 19th, 2009, 03:11 PM
|
|
Authorized User
|
|
Join Date: Oct 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
C# Delegates
Please can someone provide me with a short explanation of what a delegate is, and what it would be used for?
Why would we use delegates over a function?
Thank you
|
|

November 19th, 2009, 03:14 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
A delegate is a reference to a function, so you can call a delegate as if it were a function and it will call the function it points to.
Have you tried googling "C# delegate"?
You use them all the time but might not be aware of it - events in the .Net framework use delegates.
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

November 19th, 2009, 03:17 PM
|
|
Authorized User
|
|
Join Date: Oct 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks Sam
I have just now Googled C# Delegates, but the explanation is as vague as the explanation in Wrox Beginning Visual C#.
Why would i make a reference to a function via a delegate, why dont i just call the function directly?
|
|

November 19th, 2009, 03:21 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Because the function might not exist, or have been written when you write you code to call it, you have a 'property' which you declare as a delegate - someone else can set this property to point to their own function (e.g. an event handler) and when you call the delegate it calls their function.
If you only ever want to call one function and you are writing both the code to be called and the calling code at the same time then you wouldn't need delegates.
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

November 19th, 2009, 03:32 PM
|
|
Authorized User
|
|
Join Date: Oct 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
dude, thank you!! You're making this a whole lot simplier for me!
Uhm, could you set a property of an object to point to a function using a delegate. IE: could i add a button to a form, and using events assign a specific delgate to an onclick event, without having to add code?
|
|

November 19th, 2009, 05:16 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I don't understand what you mean by 'without adding code'.
Button1.OnClick += new MouseClickHandler(Button1_OnClick);
In the above example the OnClick event is a delegate property of the Button class. It takes a delegate of type MouseClickHandler, which can point to any method which matches the MouseClickHandler delegate signature i.e. void F(object, MouseEventArgs)
|
|

November 19th, 2009, 06:18 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
|
|
0000000000
Last edited by Bob Bedell; November 19th, 2009 at 06:21 PM..
Reason: oops
|
|

November 20th, 2009, 05:08 AM
|
|
Authorized User
|
|
Join Date: Oct 2009
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
THANK YOU!!
Hi Sam, Bob
Thank you both for your explanations - they're invaluable. To be honest, I am a new developer. I studied IT at uni, with a bit of Delphi, but in the last 5 years of work history, I have not touched on any development. I then wanted to get back into it, and decided C# is the best avenue for this... And my boss is getting me involved in transferring our current office application into WPF. I hope to also get involved in developing ASP apps (correct terminology??) and potentially BizTalk and XML/XAML.
Baby steps first.
I hope to receive further explanations from you with my future questions.
Thank you!!
Chris
PS I am relatively new to this Forum biz, but can i setup an RSS feed to this forum, even though i get immediate updates via email, i'd liked to get a grasp of RSS feeds
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Help with delegates |
mike_remember |
ASP.NET 1.0 and 1.1 Professional |
2 |
November 7th, 2006 12:33 AM |
| Delegates |
RalphJr |
C# 2005 |
1 |
April 29th, 2006 08:06 AM |
| Delegates |
pramos.21d |
C# |
1 |
April 11th, 2006 03:43 AM |
| Help with Delegates |
mike_remember |
ASP.NET 1.0 and 1.1 Basics |
4 |
October 4th, 2005 07:32 AM |
| I still Don't get Delegates |
p_nut33 |
Pro VB.NET 2002/2003 |
2 |
April 26th, 2004 01:50 PM |
|
 |