remove(C# 参考)remove (C# Reference)

remove 上下文关键字用于定义自定义事件访问器,当客户端代码订阅你的事件时将调用该访问器。The remove contextual keyword is used to define a custom event accessor that is invoked when client code unsubscribes from your event. 如果提供自定义 remove 访问器,还必须提供 add 访问器。If you supply a custom remove accessor, you must also supply an add accessor.

示例Example

以下示例显示具有自定义 addremove 访问器的事件。The following example shows an event with custom add and remove accessors. 有关完整示例,请参阅操作说明:实现接口事件For the full example, see How to: Implement Interface Events.

class Events : IDrawingObject
{
    event EventHandler PreDrawEvent;

    event EventHandler IDrawingObject.OnDraw
    {
        add => PreDrawEvent += value;
        remove => PreDrawEvent -= value;
    }
}

通常不需要提供自己的自定义事件访问器。You do not typically need to provide your own custom event accessors. 大多数情况下,使用声明事件时由编译器自动生成的访问器就足够了。The accessors that are automatically generated by the compiler when you declare an event are sufficient for most scenarios.

请参阅See also