Hi,

I'm pretty new with c#. And i'm learning now about delegates. I understand what they are (i think...) and how to use them. But i don't understand why would I need \ want to use them in the first place ?

from what i get (which is probably wrong) - a delegate is just a reference to a method...so why not call the method directly ?

And if it's about calling a few methods in one call - why not build a "Master" method that calls all the other ones ?

In other words - i think i know the WHAT and HOW...but i still don't get the WHY...very confusing...as i'm sure they're there for a good reason which i just don't seem to understand...

any help is appreciated...

tnx in adv. guys (-!

When you are building something that others will use (like the Forms class in the .NET library), you don't know what methods that will be called when things like events happen. So you use delegates. Delegates help your code be more reusable.

@Momerath: Thanks for the quick reply...hmmm...not sure i fully get it though..

Do you mean like when microsoft built the .NET library, they didn't know what code you would want to run when (for example) a button is clicked in your application ?

...so that the ButtonOnClick method is added to the button_onclick_event_handler behind the scenes by the .NET lib automatically ?

or am I still not getting it ..?

You are getting it. Events are a special type of delegate, and if you look at the generated code for your WinForms application you'll see things that look like this:

this.button1.FontChanged += new System.EventHandler(this.button1_FontChanged);
this.button1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.button1_KeyUp);

This is the code where the event handlers are added to an array of delegates that the control (in this case a Button) maintains. When those events happen, it goes through the array of delegates and calls each one. They (the people at Microsoft who wrote the .NET code) didn't know what you wanted to do (or how many things you want to do, multiple methods can be assigned to a single event) so they created a delegate (array or list, I've not checked) to handle it.

They could have said "You must have a method called XXYZZ in your code and it will be called when someone presses a button". No one would like that as you'd have tons of methods to handle events you don't care about. With a delegate array/list they can just check if it is empty, and skip over that part.

Comments
Good guidance.

@Momerath: Great!! I think i'm finally getting the hang of it...

thanks alot momerath!!

p.s.
If you know of some good sites \ tutorials to learn c# it would be great...

thanks man!!!

Hi,

I'm pretty new with c#. And i'm learning now about delegates. I understand what they are (i think...) and how to use them. But i don't understand why would I need \ want to use them in the first place ?

from what i get (which is probably wrong) - a delegate is just a reference to a method...so why not call the method directly ?

And if it's about calling a few methods in one call - why not build a "Master" method that calls all the other ones ?

In other words - i think i know the WHAT and HOW...but i still don't get the WHY...very confusing...as i'm sure they're there for a good reason which i just don't seem to understand...

any help is appreciated...

tnx in adv. guys (-!

Delegates are used extensively by Framework developers. At the following URL you can find a practical use of delegates.

Practical use of delegates

you can use delegate for Function Pointer , Like C.
or You can use for Call Back Function , Event and etc.

I was wondering all of that myself so thank you for knowing how to ask a good question and get a good answer. LOL

I use the same techniques (every time) to load data into a collection of custom classes. Sometimes, I want to display the status of what's being loaded.
If I'm using a WinForm or a Console App or an ASP.NET page, the method to display will be different, but the name of the function will always be the same because I use a delegate structure.

So something like this:

public static bool Load(this List<IDbMaster> lstMasters, Func<string, string> SayMyName, ref string strError)
{
   //...
}

...will use one delegate to call the load method of each IDbMaster in a list and will also pass the display method (SayMyName) to each load so the status can be shown on the specific platform.

I know this might be a bit complex now, but it will be one of the coolest thing you can use later.

This is indeed one of the best explanations about Delegates that I have seen on-line. Thanks to everyone who's contributed. It has been a subject that I've had difficulty understanding since I first read about them, that is, until now.

The way I see it (and I could be wrong) is that Delegates can be very useful, but depending on the application that you're developing it's possible you will never use or have a need for them.

Delegates are not very powerful indeed. As you said, it is a master method that calls other methods when a specific condition happens.

What it brings to the table is it has a subscription list. So if you do not know which functions to call while you are writing this master method, well you cannot call them in your method! So delegates enable you to define the master method and let others subscribe to it, then fire them all when appropriate.

Delegates are not very powerful indeed. As you said, it is a master method that calls other methods when a specific condition happens.

What it brings to the table is it has a subscription list. So if you do not know which functions to call while you are writing this master method, well you cannot call them in your method! So delegates enable you to define the master method and let others subscribe to it, then fire them all when appropriate.

I think you are talking about events, exclusively. Delegates only define a single method. This could also be accomplished with a collection of delegates, though.

This article has been dead for over six months. Start a new discussion instead.