Log in

Previous Entry | Next Entry

Delegate: -

Delegate is an abstractpointer to a function or method.  In other words you can create a pointer which points to a method or function and then pass that pointer wherever you wish and invoke the function / method.

Events: -

Events are higher level of encapsulation over delegates. Events use delegates internally. Delegates are naked and when passed to any other code, the client code can invoke the delegate. Event provides a publisher / subscriber mechanism model.

So subscribers subscribe to the event and publisher then push messages to all the subscribers.

Delegate differs from an Event: -

Delegate is an abstract strong pointer to a function or method while events are higher level of encapsulation over delegates. Events use delegates internally.

They differ for the below reasons:-

  • Actually, events use delegates in bottom. But they add an extra layer on the delegates, thus forming the publisher and subscriber model.

  • As delegates are function to pointers, they can move across any clients. So any of the clients can add or remove events, which can be confusing. But events give the extra protection / encapsulation by adding the layer and making it a publisher and subscriber model.

Just imagine one of your clients doing this

c.XyzCallback = null

This will reset all your delegates to nothing and you have to keep searching where the error is.

Also see another .NET interview questions video on delegate: -

Copy right www.questpond.com

Buy 600+ Real Time Interview Videos

Call +91-22-66752917 or mail us on questpond@questpond.com for more details on C#, .NET, ASP.NET, SQL Server interview question videos.

SQL Server interview question videos

Latest Month

January 2017


Powered by LiveJournal.com