DEV Community

loading...

Why Use Events Instead of Delegates in C#?

Willy David Jr
Any fool can write code that a computer can understand. Good programmers write code that humans can understand. - Martin Fowler
・Updated on ・2 min read

Did you love my post?

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:

 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();
       }
    }
 }
Enter fullscreen mode Exit fullscreen mode

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:

  WhyUseEvents why = new WhyUseEvents();
  why.MyLogging += () =>
  {
    Console.WriteLine("Hi I am subscribing to the delegate.");
  };
Enter fullscreen mode Exit fullscreen mode

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

  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!
Enter fullscreen mode Exit fullscreen mode

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.

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

       if (MyLogging != null)
       {
           MyLogging.Invoke();
       }
  }
Enter fullscreen mode Exit fullscreen mode

Solution? Yes, Events!

Team Developer solved the problem by adding the event keyword:


//public YourLoggingHere MyLogging;
public event YourLoggingHere MyLogging;

Enter fullscreen mode Exit fullscreen mode

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


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')   

Enter fullscreen mode Exit fullscreen mode

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):
why.MyLogging = null;
Enter fullscreen mode Exit fullscreen mode
  • And of course to prevent invocation of the delegates.

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

Discussion (4)

Collapse
supimon profile image
supi-mon

not sure why no one appreciate this gem of an answer. possibly no one is asking questions anymore.
Thank you.

Collapse
willydavidjr profile image
Willy David Jr Author

You're welcome! I am glad you like my post :)

Collapse
csaratakij profile image
Chatchai Saratakij

This is gem, nice explaination :D

Collapse
willydavidjr profile image
Willy David Jr Author

You're welcome! I am glad you like my post :)