Can somebody explain why the .Net framework team decided that a delegate without subscribers should be null instead of an object with an empty InvocationList? I'd like to know the rationale that led to this decision.
void DoSomething()
{
EventHandler handler = SomeEvent;
if(handler != null) //why is this null-check necessary?
{
handler(this, EventArgs.Empty);
}
}
Thanks
On the CLR level, delegate fields and event fields are regular field.
Just like string MyField
defaults to null
and not ""
, so too Action MyField
defaults to null
and not an empty Action
instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With