none
What is the difference of using Invoke and without invoke for invoking delegate references . RRS feed

  • Question

  • Lets consider the below example,

    1. Invoke the delegate reference with Invoke keyword,

                            

    public delegate double returndel(int r);

     private void button1_Click(object sender, EventArgs e)
            {
                returndel ret = CalculateArea;
                double yt = ret.Invoke(4);   
                MessageBox.Show(yt.ToString());


            }
            public double CalculateArea(int r)
            {
                return 3.14 * r * r;
            }

    2.  Invoke the delegate reference without Invoke keyword,

                

    public delegate double returndel(int r);

     private void button1_Click(object sender, EventArgs e)
            {
                returndel ret = new returndel(CalculateArea);
                double yt = ret(4);
                MessageBox.Show(yt.ToString());

            }

     public double CalculateArea(int r)
            {
                return 3.14 * r * r;
            }

    What is the difference between these two ?

    Thanks and Regards

    Bijukrishnan.BS

    Friday, February 27, 2015 4:52 AM

Answers

  • No difference if you just want to call single function.

    Try the following:

            public delegate void DoMyWork(int i);
    
            static void DoMyWork1(int i)
            {
                Console.WriteLine(i);
            }
    
            static void DoMyWork2(int i)
            {
                Console.WriteLine(i * i);
            }
    
            static void delegateTest()
            {
                DoMyWork work = new DoMyWork(DoMyWork1);
                work += new DoMyWork(DoMyWork2);
    
                work.Invoke(4);
            }

    The output would be:

    4
    16

    Friday, February 27, 2015 7:25 AM
    Answerer
  • There is no functional difference. The methid call () syntax is just a short-hand for calling the Invoke method provided by the delegate type.

    Please refer to the following book page by Jon Skeet for more information: http://csharpindepth.com/Articles/chapter2/events.aspx


    Please also remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.

    Friday, February 27, 2015 11:27 AM

All replies

  • No difference if you just want to call single function.

    Try the following:

            public delegate void DoMyWork(int i);
    
            static void DoMyWork1(int i)
            {
                Console.WriteLine(i);
            }
    
            static void DoMyWork2(int i)
            {
                Console.WriteLine(i * i);
            }
    
            static void delegateTest()
            {
                DoMyWork work = new DoMyWork(DoMyWork1);
                work += new DoMyWork(DoMyWork2);
    
                work.Invoke(4);
            }

    The output would be:

    4
    16

    Friday, February 27, 2015 7:25 AM
    Answerer
  • There is no functional difference. The methid call () syntax is just a short-hand for calling the Invoke method provided by the delegate type.

    Please refer to the following book page by Jon Skeet for more information: http://csharpindepth.com/Articles/chapter2/events.aspx


    Please also remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.

    Friday, February 27, 2015 11:27 AM