Sunday, September 4, 2011

.NET interview questions: - How does delegate differ from an event?

This is one of the important and most asked .NET interview questions. Delegate is basically an abstract strong pointer to a function or method while events are higher level of encapsulation over delegates.

Events are used by delegates internally.

Main differing reasons are as follows:-

• 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.

Think that one of your clients doing this

c.XyzCallback = null
This will reset all your delegates to nothing and you have to keep searchingwhere the error is.

See the following video on different types of collection in .NET and C#: -

Also see our tutorials on Dotnet interview questions and answers


View more author’s blog on Most asked Dotnet interview questions

No comments: