none
Delegates RRS feed

  • Question

  • Hello,

    please kindly go through the following codes - 

    public Action myactiondelegate = new Action(somemethod);

    and 

    private Action myactiondelegate;
    
    public void RegisterwithActionDelegate(Action methodtoCall)
    {
         myactiondelegate += methodtoCall;
    }

    Are these both the same ? if their functionality is the same, which one is better and why? 

    Thursday, June 13, 2019 1:26 AM

Answers

  • There is a difference between both pieces of code. The second one has the ability to add another Action to the delegate. If you call it repeatedly, the delegate will accumulate all of the actions, and when you invoke the delegate then all of the actions will be executed in sequence. This is also known as a "multicast" delegate. The first piece of code, on the other hand, only ever initializes the delegate to a single action.
    • Marked as answer by slalithp Friday, June 14, 2019 12:59 AM
    Thursday, June 13, 2019 6:25 AM
    Moderator
  • Hi    slalith,

    Thank you for posting here.

    As Alberto Poblacion suggested, Two pieces of code are not the same.

    When you execute the first piece of code, you declare an Action and bind a method to it.

    But when you execute the second piece of code, you declare a RegisterwithActionDelegate method to bind Action,

    it means that you can use the method to bind more than one Action, and it will execute all the method bound  to Action in turn. 

    Besides, I have made a sample on my side. and I hope it can help you understand it.

    Here’s the code:

    class Program
        {
            public static void Display1()
            {
                Console.Write("Hello,");
            }
            public static void Display2()
            {
                Console.Write("World! ");
            }
    
            private Action myactiondelegate;
            public void RegisterwithActionDelegate(Action methodtoCall)
            {
                myactiondelegate += methodtoCall;
            }
    
            public Action myactiondelegate2 = new Action(Display1);
    
            static void Main(string[] args)
            {
                Program p = new Program();
                p.RegisterwithActionDelegate(Display1);
                p.myactiondelegate();
                p.RegisterwithActionDelegate(Display2);
                p.myactiondelegate();
    
                Console.WriteLine();
                p.myactiondelegate2();
                p.myactiondelegate2 += Display2;
                p.myactiondelegate2();
    
                Console.WriteLine();
                Action myactiondelegate3 = new Action(delegate() //new Action() can omitted
                {
                    Console.Write("Hello,");
                });
                myactiondelegate3();
                myactiondelegate3 += delegate ()
                {
                    Console.Write("World! ");
                };
                myactiondelegate3();
    
                Console.WriteLine();
                Action myactiondelegate4 = () => Console.Write("Hello,");
                myactiondelegate4();
                myactiondelegate4 += () => Console.Write("World! ");
                myactiondelegate4();
    
    
                Console.ReadKey();
            }
        }
    Best regards

    Yong Lu



     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by slalithp Friday, June 14, 2019 12:59 AM
    Thursday, June 13, 2019 6:48 AM
    Moderator

All replies

  • There is a difference between both pieces of code. The second one has the ability to add another Action to the delegate. If you call it repeatedly, the delegate will accumulate all of the actions, and when you invoke the delegate then all of the actions will be executed in sequence. This is also known as a "multicast" delegate. The first piece of code, on the other hand, only ever initializes the delegate to a single action.
    • Marked as answer by slalithp Friday, June 14, 2019 12:59 AM
    Thursday, June 13, 2019 6:25 AM
    Moderator
  • Hi    slalith,

    Thank you for posting here.

    As Alberto Poblacion suggested, Two pieces of code are not the same.

    When you execute the first piece of code, you declare an Action and bind a method to it.

    But when you execute the second piece of code, you declare a RegisterwithActionDelegate method to bind Action,

    it means that you can use the method to bind more than one Action, and it will execute all the method bound  to Action in turn. 

    Besides, I have made a sample on my side. and I hope it can help you understand it.

    Here’s the code:

    class Program
        {
            public static void Display1()
            {
                Console.Write("Hello,");
            }
            public static void Display2()
            {
                Console.Write("World! ");
            }
    
            private Action myactiondelegate;
            public void RegisterwithActionDelegate(Action methodtoCall)
            {
                myactiondelegate += methodtoCall;
            }
    
            public Action myactiondelegate2 = new Action(Display1);
    
            static void Main(string[] args)
            {
                Program p = new Program();
                p.RegisterwithActionDelegate(Display1);
                p.myactiondelegate();
                p.RegisterwithActionDelegate(Display2);
                p.myactiondelegate();
    
                Console.WriteLine();
                p.myactiondelegate2();
                p.myactiondelegate2 += Display2;
                p.myactiondelegate2();
    
                Console.WriteLine();
                Action myactiondelegate3 = new Action(delegate() //new Action() can omitted
                {
                    Console.Write("Hello,");
                });
                myactiondelegate3();
                myactiondelegate3 += delegate ()
                {
                    Console.Write("World! ");
                };
                myactiondelegate3();
    
                Console.WriteLine();
                Action myactiondelegate4 = () => Console.Write("Hello,");
                myactiondelegate4();
                myactiondelegate4 += () => Console.Write("World! ");
                myactiondelegate4();
    
    
                Console.ReadKey();
            }
        }
    Best regards

    Yong Lu



     

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by slalithp Friday, June 14, 2019 12:59 AM
    Thursday, June 13, 2019 6:48 AM
    Moderator
  • thank you guys :) 
    Friday, June 14, 2019 1:00 AM