Why Use Events Instead of Delegates in C#?
Tags: events, delegates, csharp
Published: 2020-07-27 18:22:01

Did you love my post? <a href="https://www.buymeacoffee.com/willydavidjr"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=willydavidjr&button_colour=f25a71&font_colour=000000&font_family=Lato&outline_colour=000000&coffee_colour=FFDD00"></a>

Why do we use Events in C# instead of delegates? Is it possible to use with delegates alone? Why add events? Some of the common questions I get particularly to Junior Developers. Today, we will show some of the major reasons why. 

I divided the code to Team Developer and Team Client. Team Developer will code the delegate and Team Client will instantiate the class and subscribe to delegate.

## **Team Developer**

First lets say we want to use delegates alone using the following code:

```javascript
 public delegate void YourLoggingHere();
 public class WhyUseEvents
 {
    public YourLoggingHere MyLogging;
    public void MyMainProgram()
    {
       ExecuteValidation();
    }

    private void ExecuteValidation()
    {
       //Execution of validation and logic here
       //With some business rules etc.

       if (MyLogging != null)
       {
           MyLogging.Invoke();
       }
    }
 }
```

As you can see, Team Developer declare a "YourLoggingHere" delegate, and declare it as property named "MyLogging". Now Team Client can instantiate this class and use this property and add methods to it.

## **Team Client**

On the following example, Team Client wants to use the class and add methods to "MyLogging" delegate. They can add as many methods as they want. On this case, they only added one:

```javascript
  WhyUseEvents why = new WhyUseEvents();
  why.MyLogging += () =>
  {
    Console.WriteLine("Hi I am subscribing to the delegate.");
  };
```

Now both Team Developer and Team Client can use the code properly. But there is a catch. Team Client can directly invoke the delegate!

## **Issues**

```javascript
  WhyUseEvents why = new WhyUseEvents();
  why.MyLogging += () =>
  {
    Console.WriteLine("Hi I am subscribing to the delegate.");
  };

  why.MyLogging.Invoke();  //Team Client here can directly invoke the delegate!
```

As you can see on the above code, Team Client can directly invoke the delegate without Team Developer knowing it. It's not a good way to encapsulate your methods and properties since there are other validations to be executed first before invoking the delegate!

## **Review the Code**

Let's review the code, before you can invoke "MyLogging" delegate, there are validation and business logic first before you can finally invoke the delegate.

```javascript
  private void ExecuteValidation()
  {
       //Execution of validation and logic here
       //With some business rules etc.

       if (MyLogging != null)
       {
           MyLogging.Invoke();
       }
  }
```


## **Solution? Yes, Events!**

Team Developer solved the problem by adding the event keyword:

```javascript

//public YourLoggingHere MyLogging;
public event YourLoggingHere MyLogging;

```

Team Client cannot directly invoke the delegate. If they do, it will trigger an error as shown below:

```javascript

why.MyLogging.Invoke();

//The event 'WhyUseEvents.MyLogging' can only appear on the left hand side of += or -= (except when used from within the type 'WhyUseEvents')	

```

This is a simplified explanation on why we use events even though we can already use delegates:

* To provide encapsulation and not exposing business logic
* To prevent Team Client from clearing all assign methods to delegates (You cannot do that for events):

```javascript
why.MyLogging = null;
```
* And of course to prevent invocation of the delegates.

Cheers I hope you learn on my post and happy coding!