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.

| How do you prevent a class from overriding in .NET? | How are classes related to objects in .NET Application | How are Delegates different from Events in .NET? | How are system exceptions different from application exceptions in .NET? | How are Value Types different from Reference Types in .NET? | How can a finalize method be suppressed in .NET? | How can you call Stored Procedure in ADO.NET? | How can you force Dispose method to be called automatically in .NET? | How do you call a Base Class Constructor from Derived Class Constructor in .NET? | How do you connect your VB.NET application to SQL Server? | How do you implement Cloning in .NET? | How do you implement Façade Design Pattern in .NET? | How do you implement MVC Pattern in ASP.NET? | How do you install .NET Assembly in GAC? | How is shadowing different from overriding in .NET? | How to prevent a particular .NET DLL from being decompiled? | Illustrate Delay Signing Process of an Assembly in .NET? | What are Reference Types in .NET? | What are the advantages of C#? | What are the advantages of VB.NET? | What are the differences between Namespace and Assembly in .NET? | What are the similar features between class and structure in .NET? | What are Value Types in .NET? | What do you mean by mixed mode authentication in .NET? | What do you mean by Satellite Assembly in .NET? | What do you mean by shadowing in .NET? | What is CTS in .NET? | What is ILDASM in .NET? | What is Managed Code in .NET? | What is Manifest in .NET? | What is MSIL in .NET Framework? | What is the importance of finalize method in .NET? | What is the need for Visitor Pattern in C#? | What is the purpose of bindingRedirect tag in web.config file of .NET? | What is the purpose of CodeDom in .NET? | What is the purpose of dispose method in .NET? | What is the purpose of Ngen.exe in .NET? | What is the purpose of Strong Name in COM Components of .NET? | What is the purpose of virtual keyword in .NET? | What Object Oriented Principles can be incorporated in .NET Application? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.