Home > Differences, Tutorial for Beginners > Difference between Events and Delegates in C #

Difference between Events and Delegates in C #

Difference between Events and Delegates in C Sharp

Both are very important in event driven programming, event’s are the reason of happening and delegates are the address of function. Events are like a property and well decorated in IL so that correct method can invoke.

In .Net Delegates’ looks like an interface with a single method and you can make a call to it by delegates’ instance. In .net events are implemented by multicast delegates. By event’s you can let other people know that something going on. In other words events are the modifier which allows others class or object when something happens.

For example in my company, in my Sales class I have one property called target, and it raise an event when the target is completed, this I can do via events. Here two things will come in to pictures

1. Event source: It notifies others that something is happening

2. Subscriber: (Event handler): It will respond to the events. The classes that receive the events also known as subscriber.

Button click is the very common example of events. Button_click() is the common event (button source), but to identify the which button click has been clicked is the subscriber that you can do with the help of events arguments.

However still we have the same question in our mind, what is the actual difference or relationship between events and delegates. Why both come together (mostly). Reason is very simple, .Net all event mechanism is based on delegates, delegates plays important role between event source and event handlers. In other words we can say they pass information from event source to event handlers so that corresponding event cans response to the event.

You can declare an event ”event” keyword and to declare a delegate you can use “delegate” keyword as below

Public event <nameofth handler>;

Public delegates <type> <delegatename>;

Function call and event firing notifications are looks similar. However here is the limitation between events and delegates, delegates can fire only one method but event fire more than one delegate.

Events are more protected than delegates. Delegate is reference to a method. Event is way to access that method using that delegate. Delegate use the events to do something.

Also Read: Delegates Tutorial in C#

Post Comments Below