Link to home
Start Free TrialLog in
Avatar of ANINDYA
ANINDYAFlag for India

asked on

- = means what? in Eventhandler?

Experts
can you please tell me what is the meaning of
- = in event handler  for example in the following case
( sender as AddWidgetUI ).OnWidgetAdded -= new EventHandler ( content_OnWidgetAdded );
Thanking you,
( sender as AddWidgetUI ).OnWidgetAdded -= new EventHandler ( content_OnWidgetAdded );

Open in new window

Avatar of p_davis
p_davis

it means that instead of adding to the event handler list you are subtracting it.
Avatar of ANINDYA

ASKER

Sage p_davis
That I can see and I know .My question is what is the meaning of that ?
how it is different from
( sender as AddWidgetUI ).OnWidgetAdded += new EventHandler ( content_OnWidgetAdded );
This is my question.
It is also firing event handler and if += sign is there then also it will fire event handler .
then why to use  this.

In simple arithmetic calcuation += and -= has a simple meaning .
that is for successive addition of a term with the total of the previous result.
or successive subtration of a term with the total of the previous result.

But in Eventhandler wha t is its meaning?
Thanking you



( sender as AddWidgetUI ).OnWidgetAdded += new EventHandler ( content_OnWidgetAdded );

Open in new window

when you += an event it is added to a list, and as long as it is in that list you will continue to receive notification that the event has been invoked. when you -= an event it is removed from the event handler list so you no longer are subscribed to that event.

it is sometimes necessary to to this manually to avoid memory leaks but it is usually done automatically on a dispose of the class that is subscribed to the event.
Avatar of ANINDYA

ASKER

Sage pDavis
thanks for the reply .
Can you be please a bit more elaborate on
"subscribed to that event",
" memory leaks "," dispose of the class that is subscribed to the event.",
Thanking you
ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ANINDYA

ASKER

Experts
Thanks for the help and answering all my doubts .
Also the links which you provided are useful.
Thanking you,
Anindya