Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference Between Delegates and Events in C#
In C#, both delegates and events are used for callback mechanisms, but they serve different purposes and have distinct characteristics. Understanding their differences is crucial for proper C# programming and design patterns.
Delegates
A delegate is a type that represents references to methods with a specific signature. It acts as a function pointer that can hold references to one or more methods during runtime.
Syntax
public delegate returnType DelegateName(parameters);
Example
using System;
public delegate void MyDelegate(string message);
public class DelegateExample {
public static void Method1(string msg) {
Console.WriteLine("Method1: " + msg);
}
public static void Method2(string msg) {
Console.WriteLine("Method2: " + msg);
}
public static void Main() {
MyDelegate del = Method1;
del += Method2;
del("Hello Delegates!");
// Direct assignment overwrites previous methods
del = Method2;
del("Only Method2 called");
}
}
The output of the above code is −
Method1: Hello Delegates! Method2: Hello Delegates! Method2: Only Method2 called
Events
An event is a special kind of multicast delegate that provides additional encapsulation and security. Events are typically used for notifications in the observer pattern.
Syntax
public event DelegateName EventName;
Example
using System;
public class Publisher {
public event Action<string> SomethingHappened;
public void TriggerEvent(string message) {
SomethingHappened?.Invoke(message);
}
}
public class Subscriber {
public void OnSomethingHappened(string message) {
Console.WriteLine("Subscriber received: " + message);
}
}
public class EventExample {
public static void Main() {
Publisher pub = new Publisher();
Subscriber sub = new Subscriber();
// Subscribe to event
pub.SomethingHappened += sub.OnSomethingHappened;
pub.SomethingHappened += msg => Console.WriteLine("Lambda: " + msg);
pub.TriggerEvent("Event triggered!");
}
}
The output of the above code is −
Subscriber received: Event triggered! Lambda: Event triggered!
Key Differences
| Aspect | Delegate | Event |
|---|---|---|
| Declaration | Uses delegate keyword |
Uses event keyword |
| Access Control | Full access − can be invoked directly | Restricted access − only class that declares can trigger |
| Assignment Operators | Supports =, +=, -=
|
Only supports += and -=
|
| Parameter Passing | Can be passed as method parameter | Cannot be passed as parameter |
| Encapsulation | Less encapsulated | Better encapsulated wrapper around delegate |
Access Restrictions Example
using System;
public class AccessExample {
public Action<string> MyDelegate;
public event Action<string> MyEvent;
public void TestAccess() {
// Both work inside the declaring class
MyDelegate?.Invoke("Delegate called");
MyEvent?.Invoke("Event triggered");
}
}
public class ExternalClass {
public static void TestExternalAccess() {
AccessExample obj = new AccessExample();
// Delegate can be invoked externally
obj.MyDelegate = msg => Console.WriteLine("External: " + msg);
obj.MyDelegate("Works fine");
// Event cannot be invoked externally - compilation error
// obj.MyEvent("This won't compile");
// But can subscribe to event
obj.MyEvent += msg => Console.WriteLine("Event subscriber: " + msg);
}
}
public class Program {
public static void Main() {
AccessExample.ExternalClass.TestExternalAccess();
AccessExample obj = new AccessExample();
obj.TestAccess();
}
}
The output of the above code is −
External: Works fine Delegate called Event triggered
Conclusion
Delegates provide direct function pointer functionality with full access control, while events offer a more encapsulated notification mechanism built on top of delegates. Use delegates for callback scenarios and events for publisher-subscriber patterns where you need controlled access to the invocation list.
