What is a delegate?
Delegate is a safe pointer to a method / function. So rather than pointing to the actual function you point to a delegate and via this delegate you can invoke the methods / functions.
What is a point of a pointer when we can invoke the actual function/method?
Many times rather than pointing to actual methods , we would like to point to abstraction. For example rather than pointing to “Add(int,int)” , “Substract(int,int)” , we create an abstract delegate pointer “Operation(int,int)” and point to any of them above.
What are the uses of delegates ?
There are two basic uses of delegates :-
- Callbacks / events.
- Method and function abstraction.
What are events ?
Events are encapsulation over delegates.
What are multi-cast delegates ?
If we want one delegate to point to multiple methods / function we use multicast delegates.
What are the different ways of creating a delegate in C# ?
- Anonymous types
Below is a simple video created by www.questpond.com which explains Action, Func , Predicate , Lambda and anonymous types.