Some questions about delegates

T

Tony Johansson

Hello!

My first question is:
If I add n methods to a delegete will these be called in the same order that
they were added?

Assume I have this delegate declaration.
public delegete void stopMachineryDelegete();

Here I can create an instance of the delegete like this without using new.
private stopMachineryDelegete stopMachinery;

I can add a mehod to the delegete variable like this
stopMachinery += StopFolding;

I can as an alternative do this.
stopMachinery += StopFolding;
do this instead and add the method to the delegete at the same time
stopMachinery = new stopMachineryDelegete(StopFolding);


But it's not possible to do this.
private stopMachineryDelegete stopMachinery = new stopMachineryDelegete();
stopMachinery += StopFolding;

Why is it not possible to create an instance using default constructor.

//Tony
 
B

Bob Powell [MVP]

The targets in the InvocationList of a delegate are indeed called in the
order in which they were added.

A delegate is effectively a function pointer so, in order to create the
delegate you need to provide a method of the same signature for it to call.

The default constructor of a delegate is private for this reason, forcing
you to create one with a valid target.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
My first question is:
If I add n methods to a delegete will these be called in the same order that
they were added?
Yes.

Assume I have this delegate declaration.
public delegete void stopMachineryDelegete();

Here I can create an instance of the delegete like this without using new.
private stopMachineryDelegete stopMachinery;

No you don't. That doesn't create an instance of the delegate any more
than:

string x;

creates a new string. It declares a variable of that type, with an
initial value of null.
I can add a mehod to the delegete variable like this
stopMachinery += StopFolding;

I can as an alternative do this.
stopMachinery += StopFolding;
do this instead and add the method to the delegete at the same time
stopMachinery = new stopMachineryDelegete(StopFolding);


But it's not possible to do this.
private stopMachineryDelegete stopMachinery = new stopMachineryDelegete();
stopMachinery += StopFolding;

Why is it not possible to create an instance using default constructor.

For the same reason you can't create a new FileStream with a
parameterless constructor - it doesn't make sense. Any delegate
instance has to have one or more actions associated with it.

You *can* use anonymous methods in C# 2 to create a "no-op" instance
though:

private stopMachineryDelegate stopMachinery = delegate {};

I find this is quite handy to avoid having to perform null checks.
 
T

Tony Johansson

Hello!

One last question:
What actually is the difference apart from the syntax difference between
these two alternatives named 1 and 2.

1. Having this delegate declaration
public delegate void stopMachineryDelegete();
declare a variable of the delegate like this
private stopMachineryDelegete stopMachinery;
Here I add a method to the delegete like this
1.2 stopMachinery += StopFolding;

2. Having this delegate declaration
public delegate void stopMachineryDelegete();
declare a variable of the delegate like this
private stopMachineryDelegete stopMachinery;
Do step 1.2 in this way instead
stopMachinery = new stopMachineryDelegete(StopFolding);

My first question is:
Is this alternative "stopMachinery += StopFolding;"
exactly the same as

"stopMachinery = new stopMachineryDelegete(StopFolding);"

My second question is:
Why is it not possible to do create an instance and add the method in the
same time like this.
If I do this I get compile error.
private stopMachineryDelegete stopMachinery = new
stopMachineryDelegete(StopFolding)
I mean as I did in 2 is almost the same as what I did here.

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
One last question:
What actually is the difference apart from the syntax difference between
these two alternatives named 1 and 2.

1. Having this delegate declaration
public delegate void stopMachineryDelegete();
declare a variable of the delegate like this
private stopMachineryDelegete stopMachinery;
Here I add a method to the delegete like this
1.2 stopMachinery += StopFolding;

2. Having this delegate declaration
public delegate void stopMachineryDelegete();
declare a variable of the delegate like this
private stopMachineryDelegete stopMachinery;
Do step 1.2 in this way instead
stopMachinery = new stopMachineryDelegete(StopFolding);

My first question is:
Is this alternative "stopMachinery += StopFolding;"
exactly the same as

"stopMachinery = new stopMachineryDelegete(StopFolding);"

It is when stopMachinery is null (or unassigned) to start with.

However, there is a difference between:

stopMachinery = StopFolding1;
stopMachinery = StopFolding2;

and

stopMachinery = StopFolding1;
stopMachinery += StopFolding2;

In the first case stopMachinery ends up being a delegate instance with
one action in its list. The second line completely ignores the existing
value from the first.

In the second case stopMachinery ends up with an invocation list of two
actions.
My second question is:
Why is it not possible to do create an instance and add the method in the
same time like this.
If I do this I get compile error.
private stopMachineryDelegete stopMachinery = new
stopMachineryDelegete(StopFolding)
I mean as I did in 2 is almost the same as what I did here.

That's using the implicit "this" reference, which you're not allowed to
do during variable initializers for instance variables. In other words,
it's like doing:

public class Test
{
int x = 2;
int y = x;
}

I know it may not *look* like the same kind of thing, but it is -
because of the implicit use of "this" in both cases.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top