Member-only story
C# Delegates Revealed for Unity | Faramira

Delegates are a type that represents references to methods with a specific function signature. In short, delegates are references to methods.
Read a tutorial on Solving the 8 puzzle problem using A* (star) algorithm
An instantiated delegate can be associated with any method that has a compatible signature and return type. One can then call the method through the delegate instance. Delegates are used to pass methods as arguments to other methods. In other words, delegates are methods that you can use as variables, like strings, integers, objects etc.
Delegates allow us to create robust and complex behaviours in our Unity scripts. A delegate can be thought of as a container of a function that can be can be passed around or used as a variable. Just like variables delegates can have variables assigned to them. And these values can be changed at runtime. The only difference is that while variables contain data, delegates contain functions.
Recall the following:
- What is a function signature?
- What are arguments to function?
- What is pass by value and pass by reference?
Based on C# syntax we can create a delegate with the delegate keyword, followed by a return type and the signature of the methods that…