Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Multicast Delegate Events as Properties

The saying goes "you learn something new every day", and today is no exception.  I'm not sure how I missed this, but never less, this post shows how to gain finer control over adding and removing of event handlers to public multicast delegate events.

Typically, most C# developers are used writing event code as follows:


The above code exposes a public multicast delegate event, which client callers subscribe to by adding handlers using the += operator.  However, it is possible to define a multicast delegate event as a property, where the developer can control the adding and removal of event handlers with add and remove accessor decelerations. 

The example below defines properties for each event, and the accessor decelerations on each property manages the storage of event handlers using a private hash table. This approach is usually undertaken when a class defines many events, where the developer expects most of the events to be unimplemented, thus saving some overhead in creating multicast delegate event instances that are never assigned a handler.


Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Tuesday, October 25, 2005 5:37 PM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems