A First Look At delegates

            1. What are delegates?

            2. What we can do with the delegates?

The CLR allows defining delegates which is a type-safety way of using callback methods. Delegate indicates the signatures of a callback method.

We can combine delegates into their chains and work with this chain.  For example we can add and remove delegates from the chains. The C# also provides working with delegates with easy-to-use syntax.

 

Example:

    class Program

    {

        //Delegate indicates the signatures of a callback method

        internal delegate void Feedback(int value);

 

        static void Main(string[] args)

        {

            StaticCalling();

            InstanceCalling();

            ChainCallingDemo1(new Program());

            ChainCallingDemo2(new Program());

            Console.ReadKey();

        }

 

        private static void StaticCalling()

        {

            CallNTimes(5, new Feedback(StaticMethod));

        }

 

        private static void InstanceCalling()

        {

            CallNTimes(3, new Feedback(new Program().InstanceMethod));

        }

       

        private static void ChainCallingDemo1(Program p)

        {

            Feedback fb1 = new Feedback(StaticMethod);

            Feedback fb2 = new Feedback(p.InstanceMethod);

 

 

            //We can combine delegtes into their chains

            Feedback fbChain = null;

 

            fbChain = (Feedback)Delegate.Combine(fbChain, fb1);

            fbChain = (Feedback)Delegate.Combine(fbChain, fb2);

 

            Console.WriteLine("Chain calling after the Delegate.Combine");

            CallNTimes(1, fbChain);

 

            //We can remove delegates from their chains

            Console.WriteLine("Chain calling after the Delegate.Remove");

            fbChain = (Feedback)Delegate.Remove(fbChain, fb1);

 

            CallNTimes(1, fbChain);

        }

 

        private static void ChainCallingDemo2(Program p)

        {

            Feedback fb1 = new Feedback(StaticMethod);

            Feedback fb2 = new Feedback(p.InstanceMethod);

 

            //We can combine delegates into their chains with easy-to-use syntax

            Feedback fbChain = null;

 

            fbChain += fb1;

            fbChain += fb2;

 

            Console.WriteLine("Chain calling after the Delegate.Combine");

            CallNTimes(1, fbChain);

 

            //We can remove delegtes from their chains with easy-to-use syntax

            Console.WriteLine("Chain calling after the Delegate.Remove");

            fbChain -= fb1;

 

            CallNTimes(1, fbChain);

        }

 

        private static void CallNTimes(int n, Feedback fb)

        {

            //before delegate calling we must check its existance

            if (fb != null)

            {

                int i = 0;

                while (i < n)

                {

                    fb(i++);

                }

            }                                  

        }       

 

        private static void StaticMethod(int value)

        {

            Console.WriteLine("The StaticMethod was called: {0}", value);

        }

 

        private void InstanceMethod(int value)

        {

            Console.WriteLine("The InstanceMethod was called: {0}", value);

        }

 

    }

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