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
它们是用于添加和删除事件的编译器简写。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.