An interface can declare an event. The following example shows how to implement interface events in a class. Basically the rules are the same as when implementing any interface method or property.
To implement interface events in a class
-
Declare the event in your class and then invoke it in the appropriate places
-
public interface IDrawingObject { event EventHandler ShapeChanged; } public class MyEventArgs : EventArgs {…} public class Shape : IDrawingObject { event EventHandler ShapeChanged; void ChangeShape() { // Do something before the event… OnShapeChanged(new MyEventsArgs(…)); // or do something after the event. } protected virtual void OnShapeChanged(MyEventArgs e) { if(ShapeChanged != null) { ShapeChanged(this, e); } } }
本文介绍如何在C#中实现接口事件。通过定义一个接口IDrawingObject和一个类Shape, 展示了事件声明与触发的基本过程。
1033

被折叠的 条评论
为什么被折叠?



