Events and Delegates Explained in a Simplified way
Delegates and Pointers have always been a point of discussion among the Freshers as well as Experienced Technocrats. To simplify things for the readers, I have tried to present Events and Delegates concepts and implementation in a simplified way.
Understanding Events and Delegates
Events and Delegates are the major concepts involved in an application that involves a user interaction. In traditional applications, the use of Events and Delegates were limited to a Button click, or a Hyperlink Select or other Key board events, and Mouse events (like Left Click, Right Click and Scroll).
Then evolved the Touch events. Desktop applications, Web application and Mobile applications; the usage and varsity of Events and respective handlers i.e. Delegates grows consequently. Traditional applications were limited to Key board and Mouse Events.
The advent of Mobile applications, soft wares requiring more End user interaction; and simultaneously Silverlight and WPF emerging as major UI technologies, Events and Delegates have become an important part of the Interview questions.
Events: Events in .Net terminology implies an action performed. It may be a Key press, or a Mouse click, or Selecting an options on your smart phone.
Delegates: Delegates can be termed as Event Handlers (similar to function pointers as termed in C++). Delegate is a type that can accept a function as a parameter. We will see the following code snippets how this feature of Delegate puts it as an Event Handler.
To put it another way, a delegate is a class that can hold a reference to a method. A delegate class has a signature, and it can hold references only to methods that match its signature.
To put it more Layman terms, Delegates acts as Subscribers to Events.Programmatical implementation:
Events:
// Event Declaration for Custom Event Handler GoalReachedEventHandler
public event GoalReachedEventHandler GoalReached;
// Event Handler declaration. Takes sender and EventArgs type as
// parameter. Sender refers to the Invoker.
public delegate void GoalReachedEventHandler(object sender,
GoalReachedEventArgs e);
// Definition for Custom Event Args
public class GoalReachedEventArgs : EventArgs
{
..........
}
Delegates:
a) Delegates as Function Pointers/ Handlers
To act as a handler, the Delegate should have the same signature as that of the Function (to which the Delegate acts as a handler).
// Declaration
Delegate string OperationDelg(string s, bool b);
.....
private string OperationFunc(string str, bool bln)
{
// code
}
// OperationFunc function passed as parameter
// OperatoinFunc is registered to od. If od is invoked, OperationFunc
will be executed.
OperationDelg objDel = new OperationDelg(OperationFunc);
// Invokking the OperationDelg object
objDel("sampleDelegate", true);
b) Delegates as Event Handlers
// inbuild events and handlers
this.button1.Click +=
delegate(object sender, EventArgs e){ MessageBox.Show("Test"); };
// Custom events and Custom event handlers
Public delegate void ChangingHandler(object sender, CarArgs e);
Public event ChangingHandler Change;
......
Private void Car_Change(object sender, CarArgs ca)
{
// code
}
//add event handler
Car.Change+= new Car().ChangingHandler(car_change);
//call event
Change(this,ca);