event(C# 参考)event (C# Reference)

event 关键字用于声明发布服务器类中的事件。The event keyword is used to declare an event in a publisher class.

示例Example

下面的示例演示如何声明和引发使用 EventHandler 作为基础委托类型的事件。The following example shows how to declare and raise an event that uses EventHandler as the underlying delegate type. 有关展示如何使用泛型 EventHandler<TEventArgs> 委托类型以及如何订阅事件并创建事件处理程序方法的完整代码示例,请参阅操作说明:发布符合 .NET Framework 准则的事件For the complete code example that also shows how to use the generic EventHandler<TEventArgs> delegate type and how to subscribe to an event and create an event handler method, see How to: Publish Events that Conform to .NET Framework Guidelines.

public class SampleEventArgs
{
    public SampleEventArgs(string s) { Text = s; }
    public String Text { get; } // readonly
}

public class Publisher
{
    // Declare the delegate (if using non-generic pattern).
    public delegate void SampleEventHandler(object sender, SampleEventArgs e);

    // Declare the event.
    public event SampleEventHandler SampleEvent;

    // Wrap the event in a protected virtual method
    // to enable derived classes to raise the event.
    protected virtual void RaiseSampleEvent()
    {
        // Raise the event by using the () operator.
        if (SampleEvent != null)
            SampleEvent(this, new SampleEventArgs("Hello"));
    }
}

事件是一种特殊的多播委托,仅可以从声明事件的类或结构(发布服务器类)中对其进行调用。Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class). 如果其他类或结构订阅该事件,则在发布服务器类引发该事件时,将调用其事件处理程序方法。If other classes or structs subscribe to the event, their event handler methods will be called when the publisher class raises the event. 有关详细信息和代码示例,请参阅事件委托For more information and code examples, see Events and Delegates.

可以将事件标记为publicprivateprotectedinternalprotected internalprivate protectedEvents can be marked as public, private, protected, internal, protected internal or private protected. 这些访问修饰符定义该类的用户访问该事件的方式。These access modifiers define how users of the class can access the event. 有关详细信息,请参阅访问修饰符For more information, see Access Modifiers.

关键字和事件Keywords and Events

下列关键字应用于事件。The following keywords apply to events.

关键字Keyword 说明Description 更多相关信息For more information
staticstatic 使事件可供调用方在任何时候进行调用,即使不存在类的实例。Makes the event available to callers at any time, even if no instance of the class exists. 静态类和静态类成员Static Classes and Static Class Members
virtualvirtual 允许派生类使用重写关键字重写事件行为。Allows derived classes to override the event behavior by using the override keyword. 继承Inheritance
sealedsealed 指定对于派生类,它不再是虚拟的。Specifies that for derived classes it is no longer virtual.
abstractabstract 编译器不会生成 addremove 事件访问器块,因此派生类必须提供其自己的实现。The compiler will not generate the add and remove event accessor blocks and therefore derived classes must provide their own implementation.

可以通过使用静态关键字将事件声明为静态事件。An event may be declared as a static event by using the static keyword. 这可使事件可供调用方在任何时候进行调用,即使不存在类的实例。This makes the event available to callers at any time, even if no instance of the class exists. 有关详细信息,请参阅静态类和静态类成员For more information, see Static Classes and Static Class Members.

可以通过使用虚拟关键字将事件标记为虚事件。An event can be marked as a virtual event by using the virtual keyword. 这可使派生类使用重写关键字重写事件行为。This enables derived classes to override the event behavior by using the override keyword. 有关详细信息,请参阅继承For more information, see Inheritance. 重写虚拟事件的事件也可以为密封,指定对于派生类,它不再是虚拟的。An event overriding a virtual event can also be sealed, which specifies that for derived classes it is no longer virtual. 最后,可以声明事件为抽象,这意味着编译器将不会生成 addremove 事件访问器块。Lastly, an event can be declared abstract, which means that the compiler will not generate the add and remove event accessor blocks. 因此,派生类必须提供其自己的实现。Therefore derived classes must provide their own implementation.

C# 语言规范C# Language Specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also