Home
Do you use a helper extension method to raise events?
  v1.0 Posted at 1/05/2018 7:23 AM by Tiago Araujo
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 ;)​

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.