C# EventHandler<T> 示例

新建一个form程序,在调试窗口输出执行过程;

为了使用Debug.WriteLine,添加 using System.Diagnostics;

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace myevhd
{
    public class Headmaster
    {
        public event EventHandler<string> ActHandler;
        public void Come()
        {
            Debug.WriteLine("校长来了");
            ActHandler(null, "王校长");
        }
    }

    public class Students
    {
        public void Action_Master(object sender, string name)
        {
            Debug.WriteLine("全班起立,喊" + name + "好;");
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            Students student = new Students();
            Headmaster master = new Headmaster();
            master.ActHandler += student.Action_Master;
            master.Come();
            InitializeComponent();
        }
    }
}
相关推荐
rainFFrain6 分钟前
qt输入类控件---QComboBox/QSpinBox
开发语言·qt
2501_9411118910 分钟前
低延迟系统C++优化
开发语言·c++·算法
未来之窗软件服务23 分钟前
自建开发工具IDE(二)文件托拽读取——东方仙盟炼气期
开发语言·前端·javascript·仙盟创梦ide·东方仙盟
Hello_WOAIAI1 小时前
4.2 python多线程编程:threading 模块深度解析
开发语言·python
2501_941111991 小时前
C++中的装饰器模式变体
开发语言·c++·算法
树下水月1 小时前
python 连接hive2 数据库
开发语言·数据库·python
Tom4i1 小时前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin
凄戚2 小时前
bash和命令
开发语言·chrome·bash
Evan芙2 小时前
Bash 变量命名规则与类型使用
linux·运维·开发语言·chrome·bash
在人间负债^2 小时前
Rust 实战项目:TODO 管理器
开发语言·后端·rust