SSW Foursquare

Do you use a helper extension method to raise events?

Last updated by Brook Jeynes [SSW] 8 months ago.See history

Enter Intro Text

Instead of:

private void RaiseUpdateOnExistingLotReceived() {
  if (ExistingLotUpdated != null) {
    ExistingLotUpdated();
  }
}

...use this event extension method:

public static void Raise<t>(
  this EventHandler<t> @event,
  object sender, 
  T args
) where T : EventArgs {
  var temp = @event;
  
  if (temp != null) {
    temp(sender, args);
  }
}

public static void Raise(this Action @event) {
  var temp = @event;

  if (temp != null) {
    temp();
  }
}

That means that instead of calling:

RaiseExistingLotUpdated();

...you can do:

ExistingLotUpdated.Raise();

Less code = less code to maintain = less code to be blamed for ;)

We open source. Powered by GitHub