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();
        }
    }
}
相关推荐
好开心啊没烦恼17 分钟前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
简佐义的博客1 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
程序员爱钓鱼1 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt
Frank学习路上1 小时前
【IOS】XCode创建firstapp并运行(成为IOS开发者)
开发语言·学习·ios·cocoa·xcode
2301_805054562 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python
lsx2024062 小时前
CSS 网页布局:从基础到进阶
开发语言
蜗牛沐雨2 小时前
警惕 Rust 字符串的性能陷阱:`chars().nth()` 的深坑与高效之道
开发语言·后端·rust
2401_858286113 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
guygg883 小时前
基于matlab的FIR滤波器
开发语言·算法·matlab