Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any side effect to remove the null handler from event?

Tags:

c#

.net

I am wondering before removing the handler

OnEvent -= handler;

Do we need to determine if the handler is the null or not. When handler is null, is there any bad effect?

like image 357
user496949 Avatar asked Feb 11 '11 10:02

user496949


2 Answers

No, if handler is null it's a no-op... or at least, it will be if it's a simple call to Delegate.Remove, which has documentation including:

Returns source if value is null or if the invocation list of value is not found within the invocation list of source.

Now an actual event just has a "remove" method effectively... and that can do anything it wants. It would be a pretty poorly-implemented event which had an ill effect when passed a null handler though, as null effectively means "an empty invocation list" in delegate terms.

like image 171
Jon Skeet Avatar answered Sep 22 '22 02:09

Jon Skeet


No, this is not a problem. It's like removing a non existing item of a collection. Nothing happens.

like image 44
HCL Avatar answered Sep 23 '22 02:09

HCL