If I have this code:
genetic = new Genetic();
genetic.foundNewBestGroupTour += new Genetico.NewBestGroupTourEventHandler(genetico_foundNewBestGroupTour);
What does the +=
do?
genetic.foundNewBestGroupTour -= new Genetico.NewBestGroupTourEventHandler(genetico_foundNewBestGroupTour);
What does the -=
do?
Read up on events.
The +=
operator in this context calls the event add
accessor, while -=
calls the remove
accessor. This is usually called subscribing and unsubscribing to the event.
The usual way to implement an event is to have a backing field which holds a multicast delegate, in this case of type Genetico.NewBestGroupTourEventHandler
. The accessors mentioned add and remove from the "invocation list" of this multicast delegate field.
It's used to subscribe / unsubscribe (bind / unbind) to an event.
genetic.foundNewBestGroupTour += genetico_foundNewBestGroupTour
Subscribes (binds) an event handler so that the method genetico_foundNewBestGroupTour
will be called whenever the foundNewBestGroupTour
event is raised on genetic
.
genetic.foundNewBestGroupTour -= genetico_foundNewBestGroupTour;
Unsubscribes (unbinds) the handler. After this code is executed, the method genetico_foundNewBestGroupTour
will be no longer be called when the foundNewBestGroupTour
event is raised on genetic
.
Further Reading
- How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)
They are the compiler shorthand for adding and removing events.