Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I would be grateful if you help me to find the answer.

I have started working with ASP.NET controls and there appeared a question: "why events in ASP controls are defined as delegates, and not as methods"?

Thank you in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Dec-11 17:53pm    
Do you know any way to trigger something like events using regular methods? Really? May I ask you how?
--SA

1 solution

If looks like you have no idea how delegates work, not mentioning events and their relationship. It's hard to explain all in once, while C# and .NET manuals provides detailed explanation.

First, read this: http://en.wikipedia.org/wiki/Delegation_%28programming%29[^].

Start here: http://msdn.microsoft.com/en-us/library/ms173171%28v=VS.100%29.aspx[^]. Learn all about delegates and move here: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^]. Try to understand their relationships.

In a nutshell, delegates and events are all about inversion of control, see http://en.wikipedia.org/wiki/Inversion_of_control[^]. The method of abstraction achieved with delegates is simply impossible with methods. A delegate is a .NET way of operating "methods" (in a generalized sense of this words) as first-class citizen object, see http://en.wikipedia.org/wiki/Functional_programming[^], http://en.wikipedia.org/wiki/First-class_function[^], http://en.wikipedia.org/wiki/Higher-order_function[^]. Simply put, a method can not be assigned to a variable (a method itself, not its return value or something), but a delegate instance can be.

A delegate instance object is a complex object of some class (and this is not of the type of the corresponding delegate type), which carries an invocation list, which is a list of handlers each carrying a pointer to a code of a method and a reference to an object on which an instance method is called — "this" reference (it could be null which means the method is static). You can store the delegate instance in some variable and pass it to some method, or assign it to some type (class or structure) field or property.

Please see my article "Dynamic Method Dispatcher" where I explain it in further detail in the section "4.1 On the Nature of Delegate Instance". Note: such explanation is hard to find, so please read mine.

When you develop some class, you don't want to limit all calls with the methods of this class, and not even with the method of the class hierarchy like you would do using virtual methods. You want to say: "when something happen, I want to call some method which a user wants to supply". This is how inversion of control works here. Are you getting the picture? For further detail, read the manuals.

—SA
 
Share this answer
 
Comments
Mich_90 19-Dec-11 22:00pm    
so as I understood the reason is to achive the highest level of abstraction.
Sergey Alexandrovich Kryukov 19-Dec-11 22:46pm    
I don't know what level is "highest", but this is yet another way of abstraction used to delegate a call of a user's function in the situation when the point of the call is not accessible to the user of the calling code and when the handler should be different for different uses and/or multicast. (All delegate instances are multicast in their nature). It is also a way to customize a call triggered by a event.
--SA

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