In C#, an Event Delegate is a special type of delegate that is used to define events in a class. An event is a way for a class to notify other classes or objects when something important happens, such as a button being clicked, a file being saved, or a network connection being established.
An event delegate is typically defined using the following syntax:
public delegate void MyEventHandler(object sender, EventArgs e);
This defines a delegate type named "MyEventHandler" that takes two parameters: an object that represents the sender of the event, and an EventArgs object that contains any additional data related to the event.
To define an event in a class, you declare a private event field of the delegate type and then create two public methods: one to add event handlers to the event, and one to remove event handlers from the event. The following code demonstrates this:
public class MyClass
{
private MyEventHandler myEvent;
public event MyEventHandler MyEvent
{
add { myEvent += value; }
remove { myEvent -= value; }
}
public void DoSomething()
{
// Do something important here
// ...
// Raise the event
myEvent?.Invoke(this, EventArgs.Empty);
}
}
In this example, the class "MyClass" defines an event named "MyEvent" of type "MyEventHandler". The event is declared using the "event" keyword, which ensures that the event can only be accessed through the "add" and "remove" methods.
The "DoSomething" method is where the event is raised, using the "myEvent?.Invoke(this, EventArgs.Empty)" syntax. This calls all the event handlers that have been added to the event, passing in the "this" object as the sender and an empty EventArgs object.
To subscribe to the event in another class, you simply create an instance of the delegate type and pass in a method that matches the delegate signature. For example:
public class OtherClass
{
public void HandleMyEvent(object sender, EventArgs e)
{
// Handle the event here
// ...
}
}
// Somewhere else in the code...
MyClass myClass = new MyClass();
OtherClass otherClass = new OtherClass();
myClass.MyEvent += otherClass.HandleMyEvent;
In this example, the "OtherClass" defines a method named "HandleMyEvent" that matches the signature of the "MyEventHandler" delegate. To subscribe to the "MyEvent" event in "MyClass", an instance of "OtherClass" is created and the "HandleMyEvent" method is added to the event using the "+=" syntax.
Overall, event delegates in C# provide a powerful mechanism for building decoupled and extensible code, by allowing objects to communicate with each other through events.
Liked By
Write Answer
Event Delegate in C#.
Join MindStick Community
You have need login or register for voting of answers or question.
Ravi Vishwakarma
29-Mar-2023In C#, an Event Delegate is a special type of delegate that is used to define events in a class. An event is a way for a class to notify other classes or objects when something important happens, such as a button being clicked, a file being saved, or a network connection being established.
An event delegate is typically defined using the following syntax:
This defines a delegate type named "MyEventHandler" that takes two parameters: an object that represents the sender of the event, and an EventArgs object that contains any additional data related to the event.
To define an event in a class, you declare a private event field of the delegate type and then create two public methods: one to add event handlers to the event, and one to remove event handlers from the event. The following code demonstrates this:
In this example, the class "MyClass" defines an event named "MyEvent" of type "MyEventHandler". The event is declared using the "event" keyword, which ensures that the event can only be accessed through the "add" and "remove" methods.
The "DoSomething" method is where the event is raised, using the "myEvent?.Invoke(this, EventArgs.Empty)" syntax. This calls all the event handlers that have been added to the event, passing in the "this" object as the sender and an empty EventArgs object.
To subscribe to the event in another class, you simply create an instance of the delegate type and pass in a method that matches the delegate signature. For example:
In this example, the "OtherClass" defines a method named "HandleMyEvent" that matches the signature of the "MyEventHandler" delegate. To subscribe to the "MyEvent" event in "MyClass", an instance of "OtherClass" is created and the "HandleMyEvent" method is added to the event using the "+=" syntax.
Overall, event delegates in C# provide a powerful mechanism for building decoupled and extensible code, by allowing objects to communicate with each other through events.