What are delegates and why are they required? The delegates in .NET are like the pointers to the functions in C/C++. The difference is that these are type safe unlike the once in C/C++.
There are situations where in a programmer or an application needs to perform an action on a particular event. Eg: Some user action like click, text change, etc. So when these actions are performed by the user, the delegates invoke the respective functions.
Delegates are like type safe function pointers. We need delegates as they can be used to write much more generic functions which are type safe also.
It encapsulates the memory address of a function in the code. Events are created using delegates in .Net. When an event is published/thrown, the framework examines the delegate behind the event and then calls the function that is referred to by the delegate.
|