修饰符:event
event 关键字用于在发行者类中声明事件。
示例
下面的示例演示如何声明和引发将
C# | |
---|---|
public class SampleEventArgs { public SampleEventArgs(string s) { Text = s; } public String Text {get; private set;} // 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. SampleEvent(this, new SampleEventArgs("Hello")); } } |
本在线速查手册由www.w♡3♡x♡u♡e.com提供,请勿盗用!
事件是特殊类型的多路广播委托,仅可从声明它们的类或结构(发行者类)中调用。如果其他类或结构订阅了该事件,则当发行者类引发该事件时,会调用其事件处理程序方法。有关更多信息和代码示例,请参见事件(C# 编程指南) 和委托(C# 编程指南)。
事件可标记为
关键字和事件
下面的关键字可应用于事件。
关键字 |
说明 |
更多信息 |
---|---|---|
|
即使类没有实例,调用方也能在任何时候使用该事件。 |
|
|
允许派生类通过使用 |
|
|
指定对于派生类它不再属虚拟性质。 |
|
|
编译器不会生成 add 和 remove 事件访问器块,因此派生类必须提供自己的实现。 |
|
本在线速查手册由www.w♡3♡x♡u♡e.com提供,请勿盗用!
通过使用
通过使用