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.
Just like classes and interfaces, we can declare delegates outside of classes or nested within classes. We can mark them private , public , or internal .
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.
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With