C# event removal syntax

后端 未结 3 1159
名媛妹妹
名媛妹妹 2021-01-05 05:31

I am confused by the syntax for removing event handlers in C#.

Something += new MyHandler(HandleSomething); // add
Something -= new MyHandler(HandleSomething         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-05 05:45

    You can think of events as placeholder methods for the delegated logic that executes when the event is raised. A single event can have multiple subscribers (multi-casting), so the += and -= syntax is how a single event handler is attached or removed. Simply doing assignment would reset the event's subscriptions, which could cause unwanted side-effects.

    EDIT: this link explains more about eventing in C#

提交回复
热议问题