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();
        }
    }
}
相关推荐
freedom_1024_19 分钟前
【c++】使用友元函数重载运算符
开发语言·c++
San30.34 分钟前
深入理解 JavaScript OOP:从一个「就地编辑组件」看清封装、状态与原型链
开发语言·前端·javascript·ecmascript
郑州光合科技余经理39 分钟前
基于PHP:海外版同城O2O系统多语言源码解决方案
java·开发语言·git·spring cloud·uni-app·php·uniapp
zmzb010342 分钟前
C++课后习题训练记录Day43
开发语言·c++
wadesir1 小时前
C语言模块化设计入门指南(从零开始构建清晰可维护的C程序)
c语言·开发语言·算法
t198751281 小时前
MATLAB水声信道仿真程序
开发语言·算法·matlab
烛阴1 小时前
C#异常概念与try-catch入门
前端·c#
qq_12498707532 小时前
基于SpringBoot+vue的小黄蜂外卖平台(源码+论文+部署+安装)
java·开发语言·vue.js·spring boot·后端·mysql·毕业设计
CAE虚拟与现实2 小时前
C/C++中“静态链接(Static Linking)” 和 “动态链接(Dynamic Linking)释疑
开发语言·c++·dll·动态链接库·lib库
万邦科技Lafite2 小时前
一键获取淘宝店铺所有商品信息,实时监控商品数据
开发语言·数据库·python·api·开放api·电商开放平台·淘宝开放平台