Search (Article Or Program)

03 March 2013

How are Delegates different from Events in .NET?



Whenever you deal with events in your code, you would have used delegates to handles them.

You know that delegates and events are interrelated. But does their scope stop with each other? How are they different from one another? These questions are answered by highlighting the differences between them:
Events
Delegates
In your application, you might want an action to be performed on click of a button. This button click is an event. Likewise you might define any number of events as per your need.When an event fires, an appropriate action has to be taken. This action is triggered with the help of delegates.
An Event can trigger a set of delegates each executing a different method.Delegates can wrap and execute only the methods which have the same signature as that of the delegates.
The keyword Event is a modifier.The keyword delegate refers to a type which in turn is used as a method reference.
For creating an event, you always need a delegate. You can create your own delegate with which you have to associate your event or you can use one of the predefined delegates. Here is an example of a simple event created and triggered using a newly created delegate:
public delegate void testDelegate();
public class eventClass {
public event testDelegate testEvent;
public void triggerEvent() {
if (testEvent != null) {
testEvent();
}
}
}
public class testClass {
private static void message() {
Console.WriteLine("Event Triggered");
}
public static void Main () {
eventClass obj = new eventClass();
obj.testEvent += new testDelegate( message);
obj.triggerEvent();
}
}
Output of this code will be:
Event Triggered
In this example, you create and define an event called testEvent. This event is wrapped inside a delegate called testDelegate.
Delegates are not just used for handling events. They are also used for much other purpose. Few of them are listed below:
" Parallel Processing using Thread Communication
" Define Generic Class Libraries
" Wrap and execute any method which cannot be determined during compile time, but only at run time
" Define and execute anonymous methods
" Execute both instance method as well as static method using the same delegate
Here is a simple example of delegates which wrap an instance method and static method sharing the same signature as that of the delegate:
public delegate void testDelegate(int data);
public class sampleClass {
public static void staticMethod(int data) {
Console.WriteLine("In
staticMethod:{0}", data);
}
public void instanceMethod(int data) {
Console.WriteLine("In
instanceMethod:{0}", data);
}
}
public class testClass {
public static void Main( ) {
sampleClass obj =
new sampleClass();
testDelegate delObj = new testDelegate(sampleClass.staticMethod);
delObj += new
testDelegate(obj.instanceMethod);
delObj(30);
}
}
Output of this example will be:
In staticMethod:30
In instanceMethod:30
Events contain two accessor methods namely add method and remove method. You can override them if required.No such accessor methods are associated with delegate.
Access to events is very restricted when compared to delegates. Even if you mark an event with public modifier, other classes can only perform two activities on that event: adding an event handler and removing an event handler. Other classes are not allowed to fire the event or trace out the existing handlers of that event.Access to delegates is based on what access modifier is associated with it. If the delegate is public then it can be used to wrap methods of any classes in any project which match its signature.