---
title: "Event Delegate in C#."  
description: "Event Delegate in C#."  
author: "James Smith"  
published: 2011-03-18  
updated: 2023-03-30  
canonical: https://www.mindstick.com/forum/202/event-delegate-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Event Delegate in C#.

I wanna ask that how to create [user defined](https://www.mindstick.com/interview/23047/how-to-create-user-defined-exception) [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london) in c#.

## Replies

### Reply by Ravi Vishwakarma

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:

```cs
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:

```cs
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:

```cs
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.


---

Original Source: https://www.mindstick.com/forum/202/event-delegate-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
