There are different types in C# such as class,interfaces and enums.One of the reference type is delegate.Delegate allows to define objects which can point to a function.This makes it convenient to define decoupled application and pass function as a parameter value to other functions.
For example if you define a delegate called ProcessDelegate then you can create an object of this delegate.This delegate object can point to a method called printMessage which can be passed to a function:
You declare a delegate by using the keyword delegate followed by the signature of the method the delegate will point to:
delegate [return type] Method(parameter list);
by this understanding we can define a delegate called SimpleDelegate:
public delegate int SimpleDelegate(string message);
Now we can declare an object of this delegate as:
Now we can call this method as:
If you notice above we are calling the simpleMethod by using the delegate object.This allows to create loosely coupled application and to pass the function reference to other functions as parameters.
If we require we can define the logic to call appropriate function using delegate at runtime as: