Thursday, June 11, 2009

Thursday Quickie: Using the NULL pattern for event delegates

This little tweet turned up this morning in response to Mike Taulty wanting“the ability to fire an event without having to check it for null”.

richardblewett @mtaulty you can use the null pattern for this Mike, just do = delegate{}; on the event declaration
Thu, Jun 11 09:27:36 from Witty in reply to mtaulty


It’s pretty obvious when you think about it tho’...

public event EventHandler CriteriaChanged = delegate{};

Easy-peasey.

Update: Richard credits Andy Clymer the original idea (see comments). How far will the chain go?!