clear invokation list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am wondering how I could clear the invokation list of delegates.

I tried the following:

//make a chain
MyDel += SomeDel;

// Clear the chain
Delegate [] tmp = MyDel.GetInvocationList();
Delegate.RemoveAll(MyDel, tmp[0]);

Nothing (not even one delegate) is removed from the list.

What am I missing?

Thanks,
Lubomir
 
Hi,
delegates are immutable, you cannot change their contents, only create new
instances. The Delegate.RemoveAll method will remove all occurances of the
specified delegate from the provided delegate and return you a new delegate
instance. So in your example you want to be using the return value from the
RemoveAll method.

Thanks
Mark.
 
Lubomir said:
I am wondering how I could clear the invokation list of delegates.

You can't change an existing delegate - it's immutable.
I tried the following:

//make a chain
MyDel += SomeDel;

// Clear the chain
Delegate [] tmp = MyDel.GetInvocationList();
Delegate.RemoveAll(MyDel, tmp[0]);

Nothing (not even one delegate) is removed from the list.

What am I missing?

As I say, delegates are immutable - think of them as being like strings
in that respect.

See http://pobox.com/~skeet/csharp/events.html for more details.
 
Thanks for help.

Lubomir



Jon Skeet said:
Lubomir said:
I am wondering how I could clear the invokation list of delegates.

You can't change an existing delegate - it's immutable.
I tried the following:

//make a chain
MyDel += SomeDel;

// Clear the chain
Delegate [] tmp = MyDel.GetInvocationList();
Delegate.RemoveAll(MyDel, tmp[0]);

Nothing (not even one delegate) is removed from the list.

What am I missing?

As I say, delegates are immutable - think of them as being like strings
in that respect.

See http://pobox.com/~skeet/csharp/events.html for more details.
 

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

Back
Top