C# 进阶 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

事件声明

  事件和方法一样具有签名,签名包括名称和参数列表。事件的签名通过委托类型来定义,然后向类中添加事件需要使用 event 关键字,并提供委托类型和事件名称。例如:

public delegate void TestEventDelegate(object sender, System.EventArgs e);
public class EventSource
{
    public event TestEventDelegate TestEvent; //基于上面的委托
    private void RaiseTestEvent() { /* ... */ }
}

  事件可标记为 public、private、protected、internal 或 protected internal。这些访问修饰符定义类的用户访问事件的方式。