Delegates in C# (and in other programming languages such as Java) allow you to do things that
other languages do through leveraging function pointers. In C++ there is a feature called a callback
function that uses pointers to functions to pass them as parameters to other functions. The
main difference between delegates and function pointers is that delegates are both object-oriented
and type-safe, and the delegate encapsulates both the object instance and a method (this encapsulation
protects data from corruption by other functions because of errors in programming).
A delegate can hold references to one or more functions and invoke them as needed.
Delegates differ in other ways from function pointers:
❑
Delegates are dynamic and are declared at runtime. In C++ you had to know the function
name before you were able to use the function pointer.
❑ Delegates don’t just point to one function. Instead, they point to an ordered set of functions.
No comments:
Post a Comment