Last year I learnt a bit of Scala and I realized that functional languages have, I have to admit, a lot of pros. I am not saying that the functional paradigm is one of my favorites; it is not, in fact I think that it is one of the most difficult to interiorize. However, some of the ideas of this paradigm are really useful and they would be very useful in other languages.
And suddenly, learning C# I discovered the delegates and how to pass functions as arguments in other functions. So simple, so easy, so clean, so beautiful that I had to share it with you.
Example of delegates in C#:
You can also use an anonymous method or a lambda expression instead of an actual method:
In this example with multiple classes subscribing to a delegate, we have a class that allows the user to make movements with the arrow keys (for example, to escape from a maze) and another class that constraints the number of movements to escape:
Instead of defining a new delegate each time, you can take advantage of the delegates that come with the .Net library which are Action and Func. The first one is used when the delegate returns void and the other when a value is returned (thus not common with events). Both are overloaded for different number of arguments, from 0 up to 16 (I think it's enough), with the form: Action(T1 arg1, T2 arg2, ...).
Passing function as arguments to other functions is also very easy. Here I modify the example to prompt the user to retry the game when the max movements are reached: Note that when you use delegates in Unity3D and you apply them to a MonoBehaviour, you must be careful with their lifetime and prevent the delegate from calling methods that are not anymore available. Be aware that changing the scene or removing the component from the game object, removes the script from memory but doesn't deallocate the callback, so you should remove it explicitly in the onDestroy method.
Enjoy!
And suddenly, learning C# I discovered the delegates and how to pass functions as arguments in other functions. So simple, so easy, so clean, so beautiful that I had to share it with you.
Example of delegates in C#:
You can also use an anonymous method or a lambda expression instead of an actual method:
In this example with multiple classes subscribing to a delegate, we have a class that allows the user to make movements with the arrow keys (for example, to escape from a maze) and another class that constraints the number of movements to escape:
Instead of defining a new delegate each time, you can take advantage of the delegates that come with the .Net library which are Action and Func. The first one is used when the delegate returns void and the other when a value is returned (thus not common with events). Both are overloaded for different number of arguments, from 0 up to 16 (I think it's enough), with the form: Action(T1 arg1, T2 arg2, ...).
Passing function as arguments to other functions is also very easy. Here I modify the example to prompt the user to retry the game when the max movements are reached: Note that when you use delegates in Unity3D and you apply them to a MonoBehaviour, you must be careful with their lifetime and prevent the delegate from calling methods that are not anymore available. Be aware that changing the scene or removing the component from the game object, removes the script from memory but doesn't deallocate the callback, so you should remove it explicitly in the onDestroy method.
Enjoy!