Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing delegates from chain

Tags:

c#

.net

clr

class Program
{
    internal delegate int CallBack(int i);

    static void Main(string[] args)
    {
        CallBack callbackMethodsChain = null;
        CallBack cbM1 = new CallBack(FirstMethod);
        CallBack cbM2 = new CallBack(SecondMethod);

        callbackMethodsChain += cbM1;
        callbackMethodsChain += cbM2;

        Delegate.Remove(callbackMethodsChain, cbM1);
    /*L_0039: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class  [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
        L_003e: pop 
        L_003f: ldloc.0 */

        Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);
        //Output: 2 **WTF!!!**


        callbackMethodsChain -= cbM1;
        /*
    L_0054: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class   [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
          L_0059: castclass Generics.Program/CallBack
          L_005e: stloc.0 
          L_005f: ldloc.0 
        */
        Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);
        //Output: 1
    }

    private static int FirstMethod(int test)
    {            
        Trace.WriteLine("FirstMethod");
        return test;
    }

    private static int SecondMethod(int test)
    {
        Trace.WriteLine("SecondMethod");
        return test;
    }
}

So, we always need to cast (CallBack)Delegate.Remove(callbackMethodsChain, cbM1); to remove delegate from chain. It's not obviously.

like image 603
Grigory Bushuev Avatar asked Nov 11 '09 08:11

Grigory Bushuev


People also ask

Can delegates be private?

Just like classes and interfaces, we can declare delegates outside of classes or nested within classes. We can mark them private , public , or internal .

What is meant by multicast delegate?

The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The - operator can be used to remove a component delegate from a multicast delegate.

Which are based on delegates and are multicast delegates?

Multicast delegates help to invoke multiple callbacks. Events encapsulate delegate and implement publisher and subscriber model. Events and Multicast are types of delegates. So delegate is the base for events and multicast.

What are delegates why delegates are used explain the concept of multicast delegates using suitable example?

Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.


2 Answers

A delegate is immutable, which means you cannot change it. Any methods that seems to change it, like "adding" to it or "subtracting" from it, actually returns a new delegate with the changes.

So this would not work:

a.Remove(b);

But this would:

a = a.Remove(b);

in terms of calling the Remove method that is.

Note that the following syntax does the right thing:

a -= b;

This is why, after calling Remove, that you still observe the code calling the delegate you seemingly removed, you're still calling the original delegate chain with that delegate present.

like image 81
Lasse V. Karlsen Avatar answered Oct 12 '22 12:10

Lasse V. Karlsen


Some Other points are

Duplicates are allowed in your delegate i.e your delegate can have something like [cbM1,cbM2,cbM2,cbM3]

If u have method group [cbM1,cbM2, cbM3,cbM4,cbM5,cbM1,cbM2] and you perform some operation like [cbM1,cbM2, cbM3,cbM4,cbM5,cbM1,cbM2] - [cbM1,cbM2] then you will get [cbM1,cbM2, cbM3,cbM4,cbM5]

If u have [cbM1,cbM2, cbM3,cbM4,cbM5] and you perform some operation like [cbM1,cbM2, cbM3,cbM4,cbM5]-[cbM1,cbM5] you will get [cbM1,cbM2, cbM3,cbM4,cbM5]

like image 26
Tinku Avatar answered Oct 12 '22 13:10

Tinku