0

I'm wondering what's is the difference between this two uses of Add listener on c#. On my script, both are working. But I guess there should be a difference ? Thanks !

btn.onClick.AddListener(() => PickAPuzzle()); 

btn.onClick.AddListener (PickAPuzzle); 
Niharika
  • 1,188
  • 15
  • 37
tieum67
  • 23
  • 4

1 Answers1

1

The two are identical, the first is using a slightly longer form of passing an explicit overload whereas the second is using a method group, where the compiler chooses the correct overload (see: What is a Method Group in C#?).

AddListener() is one and the same, and the delegates they receive are effectively the same. The difference is syntactical shorthand.

Community
  • 1
  • 1
STW
  • 44,917
  • 17
  • 105
  • 161
  • 1
    Isn't the first line a delegate to a method which will call the method, and the second a direct delegate to the method ? if so, won't the second line be (very, very slightly) more efficient ? – gobes Feb 23 '17 at 11:29
  • 1
    @gobes another consideration is if compiler will optimize this and produce the same compiled code – Sharky Feb 23 '17 at 12:21
  • 1
    @gobes I misspoke a bit. Both effectively produce the same delegate--one is more explicit, the other less so. – STW Feb 23 '17 at 15:33