Use of delegates in C #


I have the following code, taken from a Unity script to play video:

public delegate void VideoEnd();
public VideoEnd OnEnd;

Then, when a video ends, the following event is generated:

if (OnEnd != null)

What I need, is (not if you declare, or what would be the name of what I want to do) assign OnEnd the Stop () function, that is, when the delegate event occurs, generate a stop What is the correct method to use delegates in this case?

asked by Christian Barcelo 13.03.2017 в 18:13

1 answer


According to the example of your code what you should do is this:

OnEnd = Stop;

The only rule for this to work is that Stop is declared as

void Stop(){
   // Aqui va la implementacion

If you want OnEnd to stop sending Stop call, you only assign OnEnd to null

OnEnd = null;
answered by 13.03.2017 / 20:00