C# EventHandler<T> 示例

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

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

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 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();
        }
    }
}

C# EventHandler<T> 示例_第1张图片 

你可能感兴趣的:(.Net,c#,开发语言)