Search Unity

Question Are methods auto-converted to delegates when added directly to a delegate list

Discussion in 'Scripting' started by Unideer, Mar 29, 2023.

  1. Unideer

    Unideer

    Joined:
    Feb 24, 2023
    Posts:
    21
    I want to put methods in a list. But since lists can only hold objects, I need to first assign the methods to delegates:

    void MyMethod1(){
    //code
    }

    Action myDelegate1 = MyMethod1;


    Then I can add the delegate to the list:

    List<Action> myDelegList = new List<Action>();
    myDelegList.Add((myDelegate1));


    However, I found you can just:

    myDelegList.Add((MyMethod1));


    i.e. adding the method directly to the delegate/action list

    So has MyMethod1 been auto-converted to a delegate when added to myDelegList?
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,987
    Yes. :) Didn't we just discussed that in your other question?

    A method is not a datatype on its own, a delegate is a datatype that can be passed around.
     
    Unideer likes this.
  3. Unideer

    Unideer

    Joined:
    Feb 24, 2023
    Posts:
    21
    Hey thanks a lot for your guy's help on the other thread. I found I have phrased my question really terrible so I started this new thread.

    Just wanted to make sure if a delegate list is actually storing the converted delegate class object (like you explained) after being fed a method. Like the method has been magically converted behind the scene xD

    You must have explained it though, just wanted to make sure.

    but thanks again