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 允許衍生類別使用 override 關鍵字覆寫事件行為。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.

事件可使用 static 關鍵字宣告為靜態事件。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.

事件可使用 virtual 關鍵字標示為虛擬事件。An event can be marked as a virtual event by using the virtual keyword. 這可讓衍生類別使用 override 關鍵字覆寫事件行為。This enables derived classes to override the event behavior by using the override keyword. 如需詳細資訊,請參閱繼承For more information, see Inheritance. 事件覆寫虛擬事件也可以 sealed,指定它對衍生類別不再是虛擬。An event overriding a virtual event can also be sealed, which specifies that for derived classes it is no longer virtual. 最後,您可以宣告事件為 abstract,也就是編譯器不會產生 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