Home c# Why delegates in Unity? C #

Why delegates in Unity? C #




In the official Manual Unity there is such an example of delegates

Public Class Delegatescript: monobehaviour
MyDelegate MyDelegate;
Void Start ()
  MyDelegate = Printnum;
  MyDelegate (50);
  MyDelegate = DoubleNum;
  MyDelegate (50);
Void Printnum (Int Num)
  Print ("Print Num:" + Num);
Void DoubleNum (int num)
  Print ("Double Num:" + Num * 2);

is not entirely clear – after all, this conclusion we can get elementary

void start ()
  Printnum (50);
  DoubleNum (50);

Please explain why they are here and are needed in such situations in general;

Answer 1, Authority 100%

For the same reason, on which they are in C #.

You can read MSDN and understand their need and why they were invented.


Delegate is a comfortable wrapper over pointers to the functions that are in C++.

For example, you can make one of the arguments of the delegate method.

When calling methods, we transmit a delegate to the method, which indicates the desired function, and inside the method we can call this delegate.

Delegates are used in the events.


And in general, Unity uses all C # language capabilities and is obvious that almost all of the functionality is available in Unity.

And use or not to use the entire ability to solve you.

With the same success you can, how to use concise possibilities
Linq , which yourself in il code will generate the necessary code, and you can use Foreach and organize collection, but it will be somewhat less concise .. .

Ie language gives you opportunities, but you dispose of them.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions