More about delegates

T

Tony Johansson

Hi!

My qestions here is just for understanding
Assume I have a delegates and I add two methods to this delegates.
When I call this delegete these two methods are called.

Now to my question is it possible to in any a way when calling the delegate
to say call only the first of the methods that was added to the delegate ?

//Tony
 
A

Andy O'Neill

Tony Johansson said:
Hi!

My qestions here is just for understanding
Assume I have a delegates and I add two methods to this delegates.
When I call this delegete these two methods are called.

Now to my question is it possible to in any a way when calling the
delegate to say call only the first of the methods that was added to the
delegate ?

//Tony
Don't add the second method.

It's rather like asking
If I hit my hand with a hammer, can I make it stop hurting.
Sure, don't hit your hand with the hammer.
 
M

mick

Tony Johansson said:
Hi!

My qestions here is just for understanding
Assume I have a delegates and I add two methods to this delegates.
When I call this delegete these two methods are called.

Now to my question is it possible to in any a way when calling the
delegate to say call only the first of the methods that was added to the
delegate ?

Unsubscribe from the method(s) you dont want:). As I understand it all the
methods
in the invocation list are called in a foreach behind the scenes.

mick
 
A

Arne Vajhøj

My qestions here is just for understanding
Assume I have a delegates and I add two methods to this delegates.
When I call this delegete these two methods are called.

Now to my question is it possible to in any a way when calling the delegate
to say call only the first of the methods that was added to the delegate ?

Remove it before calling.

Arne
 
P

Peter Duniho

Tony said:
Hi!

My qestions here is just for understanding
Assume I have a delegates and I add two methods to this delegates.
When I call this delegete these two methods are called.

Now to my question is it possible to in any a way when calling the delegate
to say call only the first of the methods that was added to the delegate ?

Yes. All delegate types inherit the base System.Delegate type, which
has the GetInvocationList() method, which returns an array of delegate
instances that are the individual delegates that will be invoked when
the delegate is invoked.

As others have pointed out, in general if you want to manipulate the
invocation outcome of a delegate, the right way to do it is to add or
remove delegate instances as appropriate. But if you temporarily want
to modify the behavior or inspect how the delegate is currently
constructed, you can use GetInvocationList() to look at the individual
delegates making up the whole delegate.

Pete
 

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

Top