The delegates chaining

  1. What does the delegates chaining mean?
  2. What is happening when we add first delegate to a chain?
  3. What is happening when we add second delegate to the chain?
  4. What is happening when we add third etc delegates to the chain?
  5. What is happening when we call the Invoke method of the chain?
  6. How does the CLR is looking for matches when we remove delegates.
  7. What is happening when we remove a delegate from a chain and there are multiple items?
  8. What is happening when we remove a delegate from a chain and there is only one item?
  9. What is happening when we remove the last delegate from a chain?

The delegates chaining provides the ability to invoke a collection of methods represented by the delegates in the chain.

 

When we add first delegate to the chain it references to the first delegate.

When we add delegates to the chain the chain references to the new crated delegate, because every time when we add a delegate to the chains the CLR creates new chains delegate.

When we add yet another delegate to the chain the chain references to yet another delegate and previous delegate becomes a good candidate for garbage collection.

When we invoke the chain it iterates through all delegates in the chain and call delegates methods, but it returns only the last return value.

Example 1:

 

    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);

CallBack cbM3 = new CallBack(ThirdMethod);

 

//We can see that when we add first delegate to the chain it references to the first delegate.

callbackMethodsChain = (CallBack)CallBack.Combine(callbackMethodsChain, cbM1);

Trace.WriteLine(callbackMethodsChain.Equals(cbM1));

 

//When we add other delegates to the chain the chain references to another object,

//because every time when we add a delegate to the chains the CLR creates new chains object.

callbackMethodsChain += new CallBack(SecondMethod);

Trace.WriteLine(callbackMethodsChain.Equals(cbM1));

Trace.WriteLine(callbackMethodsChain.Equals(cbM2));

 

callbackMethodsChain += cbM3;

Trace.WriteLine(callbackMethodsChain(5));

        }

 

        private static int FirstMethod(int test)

        {           

            Trace.WriteLine("FirstMethod");

            return test;

        }

 

        private static int SecondMethod(int test)

        {

            Trace.WriteLine("SecondMethod");

            return test;

        }

 

        private static int ThirdMethod(int test)

        {

            Trace.WriteLine("ThirdMethod");

            return test;

        }

    }

Output:

True

False

False

FirstMethod

SecondMethod

ThirdMethod

5

Example 2:

    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);

            CallBack cbM3 = new CallBack(ThirdMethod);

 

            callbackMethodsChain += cbM1;

            callbackMethodsChain += cbM2;

            callbackMethodsChain += cbM3;

 

            //When we remove a delegate from chain and match is found and there are multiple items,

            //the chain will reference to the new created delegate.

            callbackMethodsChain -= cbM1;

            Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);

 

            //When we remove a delegate from chain  and match is found and there is only one item left,

            //the returned delegate will reference to the last delegate in the chain.

            callbackMethodsChain -= cbM2;

            Trace.WriteLine("The number of delegates in chain", callbackMethodsChain.GetInvocationList().Length.ToString());

            Trace.WriteLine(callbackMethodsChain == cbM3);

 

            //When we remove last delegate from chain and match is found

            //the returned delegate will reference to the null.

            callbackMethodsChain -= cbM3;

            Trace.WriteLine(callbackMethodsChain == null);

        }

 

        private static int FirstMethod(int test)

        {           

            Trace.WriteLine("FirstMethod");

            return test;

        }

 

        private static int SecondMethod(int test)

        {

            Trace.WriteLine("SecondMethod");

            return test;

        }

 

        private static int ThirdMethod(int test)

        {

            Trace.WriteLine("ThirdMethod");

            return test;

        }

    }

Output:

2

False

False

1: The number of delegates in chain

True

True

When we remove delegates from a chain the clr is looking for matches by Target and Method properties.

When we remove delegate from a chain and match is found and there are multiple items, the chain will reference to the new created delegate.

 

When we remove delegate from a chain  and match is found and there is only one item left, the returned delegate will reference to the last delegate in the chain.

 

When we remove the last delegate from a chain and match is found the returned delegate will reference to the null.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s