Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between static event handler and non-static event handler

Tags:

c#

.net

Is there big difference between those two?

like image 896
user496949 Avatar asked Nov 26 '10 02:11

user496949


People also ask

What is the difference between event and EventHandler?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.

What does static event mean?

With a static event, there is no sending instance (just a type, which may or may not be a class). There still can be a recipient instance encoded as the target of the delegate.

What is the difference between an event handler and an event listener?

Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.


2 Answers

Semantically there are no differences, however using static event handlers can (if you're not careful) lead to memory leaks. See this article for more info.

I've come across this problem myself, trying to use a static event handler to keep an application-wide data source up to date; the event handler was preventing my BindingSource components from being disposed, leading to all sorts of weird problems...

like image 175
Bradley Smith Avatar answered Oct 24 '22 18:10

Bradley Smith


Effectively none. All it means is that when the handler is static, there will be no this in scope (as with all static methods).

like image 23
spender Avatar answered Oct 24 '22 17:10

spender