Click here to Skip to main content
15,911,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What were the lacks of Pointer To Function so that we moved to Delegates and Events in c#
Posted

Function pointers served well in the good old days of C. But in object oriented programming languages you want to be able to store pointers to member functions. Support for that was built into C++. The problem with such member function pointers is that you always need two things to make a function call:

- the member function pointer
- a class object on which to apply them.

It is obviously kind of inellegant to keep these two things together or pass them as two separate parameters. So why not bundle them together? ... And the delegate was born. Basically, a delegate is just a compound object of an object pointer plus a member function pointer. Delegates make it easy to give another component of your software a callback point in the form of a class object and member function. This principal is used heavily in windowing frameworks. Here you wish to be able to direct certain window messages to certain member functions of the window object.

In a nutshell, the role of function pointers in the simple procedural world, has been taken by delegates in the object oriented world.
 
Share this answer
 
Lets forgive ourselves for the next few minutes for not using the officially correct phrasing: A delegate IS the function pointer of the object oriented languages. The delegate has two "sides": One side is used by the caller, and the other side is used/implemented by something that receives the call. On the "call" side the delegate looks like a normal function/function pointer and it has nothing to do with objects (!!!). The other side (the actual delegate implementation) of the delegate relays the incoming calls to "something". This "something" can, in fact, be a normal function (or the function of object oriented languages: a "static method") and not only an instance method in some languages!!! The ability of a delegate to point not only to a normal function but also to a method of an object (an instance) is just an extension to function pointers. Anyway, a method is not much more than a normal function: you can think of it as a normal function that receives an extra hidden parameter (this, self, whatever...). You call an array function like Array_GetSize(array_pointer) while you are calling an array METHOD like array_pointer->GetSize(), we just passed the hidden this pointer (array_pointer) differently in case of a method, not in the parameter list but with the "array_pointer->" prefix. When a delegate points to an instance method then the following happens: The delegate itself stores a "function pointer" to the method that receives exactly the same parameters as the call side of the delegate but in addition the method expects an extra this pointer. For this reason the delegate must store the value of this extra hidden this parameter next to the method/function pointer when the delegate points to a method instead of an old-school function because the caller of the delegate will not provide it and the delegate need it when the incoming call is relayed by using the method pointer. The extra hidden this pointer is provided by the creator of the delegate who knows the value of the hidden this pointer (the instance that will receive the event with one of its methods). The caller of the delegate is not interested about the value of the hidden this parameter (about the object/instance that receives the event).

EDIT: An event is like a collection of function pointers (sorry delegates). It provides the same "function-like interface" as the "call-side" of the delegate. When you fire the event it just relays the call to all delegates that are in the collection. Its like an array of listeners. What is different from older languages: new languages provide you with syntax that make it easier for you to declare and use "function pointers" and use "arrays of function pointers".
 
Share this answer
 
v6
It's not a "lack" as such, it's just a different way of thinking about things.
C is a procedural language, C# is event driven. The Event handling requires a way to "point" at a function, but since pointers are considered dangerous in C# (for good reasons) the Delegate was created to take that role. It is in essence a function pointer, but with class instance awareness built in: if you execute a Delegate function, you have access to the properties, fields and methods of the class instance on which it it called.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900