Yesterday I was reviewing some code created by the Prism team. I came across a rather clever event implementation that I hadn't see before:
public event EventHandler Updated = delegate { };
protected void UpdatePrice(string tickerSymbol, decimal newPrice, long newVolume)
{
_priceList[tickerSymbol] = newPrice;
_volumeList[tickerSymbol] = newVolume;
Updated(this, new MarketFeedEventArgs(tickerSymbol, newPrice, newVolume));
}
Normally I would write this code like so:
public event EventHandler Updated;
protected void UpdatePrice(string tickerSymbol, decimal newPrice, long newVolume)
{
_priceList[tickerSymbol] = newPrice;
_volumeList[tickerSymbol] = newVolume;
if(Updated != null)
Updated(this, new MarketFeedEventArgs(tickerSymbol, newPrice, newVolume));
}
I commented that this wasn't the way I was used to seeing events declared or fired and that I thought it might confuse some people. Ayende informed me that this is the way he normally does things, stating that it was threadsafe and no fuss. I'm thinking about adopting this technique and was wondering if I'm the only one who hasn't seen this before now. Also, does anyone have any opinions one way or the other about this particular implementation?
Posted
03-20-2008 12:17 PM
by
Rob Eisenberg