[Edit=gfoidl] Abgeteilt von eigene event erstellen [/url]
Hallo,
da wir schon bei Events sind schreibe ich mal 2 "Tricks" damit der Umgang mit dem Null-Check einfacher wird.
Funktioniert wunderbar
mfG Gü
Hallo,
da wir schon bei Events sind schreibe ich mal 2 "Tricks" damit der Umgang mit dem Null-Check einfacher wird.
- Der immer selbe Check kann in eine Erweiterungsmethode verpackt werden.
[highlight=c#]
public static class EventExtensions
{
public static void Fire(this EventHandler handler, object sender)
{
if (handler != null)
handler(sender, EventArgs.Empty);
}
public static void Fire<T>(this EventHandler<T> handler, object sender, T eventArgs)
where T : EventArgs
{
if (handler != null)
handler(sender, eventArgs);
}
}
public class Foo
{
public event EventHandler MyFooEvent;
public void DoSomething()
{
this.MyFooEvent.Fire(this);
}
}
[/highlight] - Der Null Check kann durch das Zuweisen eines (sinnlosen) Delegaten ganz entfernt werden.
[highlight=c#]
public class Foo
{
public event EventHandler MyFooEvent = delegate { };
public void DoSomething()
{
this.MyFooEvent(this, EventArgs.Empty);
}
}
[/highlight]
Funktioniert wunderbar
mfG Gü
Comment