Really it is a Qustion not a topic.
Q .should we have a null test before invoking a delegate?
A: suppose we have an event in a class, we need to add a null test before you call the delegate. Typically, we would write:
if (Click != null) Click(arg1, arg2);
There is actually a possible condition - the event can be cleared between the first and second line. we would actually want to write:
ClickHandler handler = Click;
if (handler != null) handler(arg1, arg2); usually. we might want to do some other sort of synchronization in other scenarios.
So back to the main question. Why is the null test required?
We can't change the existing behavior of calling through a delegate as some apps may depend on it, so it would have to be an addition to the language.
We have talked about adding an Invoke() keyword to the language to make this easier, but after a fair bit of discussion, we decided that we couldn't do the right thing all the time, so we elected not to do anything.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|