29 March 2015

difference between events and delegates

Events and delegates are very important programming design concepts when you start doing your low-level design and class level diagrams. So it's very crucial to understand what each of them and very important to understand the difference between them. In this post let's try to understand difference between events and delegates.

If you look patiently at events and delegates you will realize with both events and delegates you can add matching methods to them and upon on invoking either of a delegate or an event, the respective methods will be get called. So then what is the difference between events and delegates?

You are thinking now!.. don't worry soon you will understand difference.. continue reading...

#1 difference between events and delegates

EVENTS can only be raised from the class in which they have defined whereas your delegates don't have such restriction. DELEGATES can be invoked from both inside the class in which they defined and also from outside as well.

So now the point is, if you want to protect invocation of subscribed methods, you will choose events so that no one attempts directly to raise an event from outside the class. If this is not what you are looking at them consider delegates.

#2 difference between events and delegates

WITH EVENTS you cannot assign altogether a difference value to it, you can only add or remove subscription method to it. Let's consider an example, BalanceChanged event is declared the below class.

public class BankAccount{
public event BalanceChangedEventHandler BalanceChanged;
}
Code Listing A
BankAccount account = new BankAccount();

// INVALID assignment to event
// you can't assign 'null' to event
account.BalanceChanged = null

// INVALID assignment to event
// you can't directly assign event subscription 
// method to event 
account.BalanceChanged = BalanceChangedEventHandlingMethod;
Code Listing B
BankAccount account = new BankAccount();

// VALID subscription to event
account.BalanceChanged+=BalanceChangedEventHandlingMethod;

// VALID un subscription to event
account.BalanceChanged-=BalanceChangedEventHandlingMethod;

With events you can add subscription methods or remove subscription methods, you cannot assign null to an event. So Listing B is valid and Listing A is invalid.

With DELEGATES, you can assign null to it and directly assign any method to it apart from subscribing and unsubscribing methods. So even thing in below code listing is valid.

public class BankAccount{
public delegate string GetLanguageLiteralText(string key);
}

BankAccount account = new BankAccount();

// VALID, you can assign 'null' to a delegate.
account.GetLanguageLiteralText = null;

// VALID, you can directly 
// assign a method to a delegate
account.GetLanguageLiteralText = GetResourceTextViaWebservice;

// You can subscribe to delegate and unsubscribe to delegate
account.GetLanguageLiteralText += GetResourceTextViaWebservice;
account.GetLanguageLiteralText -= GetResourceTextViaWebservice

So now I think you aware not just events and delegates but also difference between them and you feel more confident while choosing between an event or a delegates your design.

As always add your comments guys, that will keep me writing blog spots.

1 comment:

  1. You have some really good ideas in this article. I am glad I read this. I agree with much of what you state in this article. Your information is thought-provoking, interesting and well-written. Thank you.

    www.imarksweb.org

    ReplyDelete