- = 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

ANINDYAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
it means that instead of adding to the event handler list you are subtracting it.
ANINDYAAuthor Commented:
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

p_davisCommented:
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.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ANINDYAAuthor Commented:
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
p_davisCommented:
subscribed == hook --

+= subscribes/hooks/adds

here are a couple of links that go more into depth. specifically we are dealing with delegate adding and removal.

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/wch5w409%28v=vs.71%29.aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vikram Singh SainiSoftware Engineer cum AD DeveloperCommented:
For more about -= refer to link's Code Discussion part:

http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx
Najam UddinCommented:
+=  is used to register for a event, say u have a button and u want ur function to be called on its click event
u write button1.click += <your function>

but now at any particular case you want that this function should not be called ever after
u unregistered it by -=

once you u have unregistered your function it will not be called on that event

To check create form, add two button
on click of first show message box, on click of other unregister click of first button,

run it, click first messagebox will come, click second then click first no there will be no meesagebox..
ANINDYAAuthor Commented:
Experts
Thanks for the help and answering all my doubts .
Also the links which you provided are useful.
Thanking you,
Anindya
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.