Home  /  Questions  /  Question



59   95.0
Sep 03, 2010


Delegate (C#) versus Observer Pattern?

I have a question: Is the delegate in C# equivalent to the Observer Pattern?  If not, what is the difference?

Thanks in advance.



1,364   100.0
Sep 03, 2010
In a way delegates and events can be used to implement observer pattern.  You'll find some useful discussions here

http://stackoverflow.com/questions/32034/in-c-isnt-the-observer-pattern-already-implemented-using-events

  and here

http://www.c-sharpcorner.com/UploadFile/fbulovic/ObserverNETEventFB12052005041517AM/ObserverNETEventFB.aspx

Thanks.

100   96.6
Sep 06, 2010
In C# Langaugage, Microsoft has provided the facility of multi-cast delegate to implement the Observer Pattern.  As Rajesh has mentioned there are sites which provides the implementation of same.

At the same time there is something you need to be aware off when using the multi-cast delegate. If the user has assigned, say two delegates and then assigns a null value, it will be treated as un-assigned and both previously assigned delegates will not execute.

- Saurabh