Tuesday 17 January 2012

Difference between event and delegate

Difference between event and delegate?

Event:

1) It is a data member of a type(class/structure)
2)It is declared inside a type(class/structure)
3) It is used to generate notifications which are then passed to methods though
delegates.

delegate:

1)It is a datatype(reference type) that holds references of methods with
some signatures.also called as function pointer.
2)It may or may not be declared inside a class.
3)It is used as the return type of an event and used in passing messages from event to methods.


event-->delegate-->method

example:

namespace dd
{
//delegate declaration
delegate void first();
class cc
{
//event declaration
public first myevent;
}
}

example 2:
button1.Click+=new EventHandler(this.button1_Click);
(Windows Applications)

Click is the event that returns an instance of the EventHandler delegate.
EventHandler delegate has the reference of button1_Click event and that
helps in the communication betwen the Click event and button1_Click method

No comments:

Post a Comment