We often use event handler in C#, just as below:
some_event+=some_event_handler;
Does this happen at compile time or run time? What if the some_event is static member? AFAIK, the some_event contains nothing but the entry address of some_event_handler, and the method address of some_event_handler could be determined at compile time. If some_event is a static member, could the value of the some_event be determined at compile time? I konw that if the some_event is an instance member, it's value will be setup at the creation time of the object instance. Correct me if I'm wrong.
Many thanks, guys~ :)
We can say it concerns delegates
. A delegate
variable is assigned a method dynamically. This gives us ability to write plugin methods.
Concerning instance and static method targets just a note from C# in Nutshell
When a delegate object is assigned to an instance method, the delegate object must maintain a reference not only to the method, but also to the instance to which the method belongs. The System.Delegate class’s Target property represents this instance (and will be null for a delegate referencing a static method).
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