C# 事件声明与简单使用

1 如何自定义事件?

1.1 什么是事件?

事件:事件是能够让类或对象具备通知能力的成员,站在事件的拥有者的角度,事件就是用来通知别人的工具

1.2 自定义事件

现在有一个例子
Jeff 准备告诉他的老师他的名字和学号

自定义声明事件

using System;

namespace EventTest
{
    class EventDemo
    {
        static void Main(String[] args)
        {
            // student : 事件的拥有者
            Student student = new Student();
            // teacher : 事件的响应者
            Teacher teacher = new Teacher();
            // student.Test : 事件
            // teacher.Listen : 事件处理器
            // student.Test += Teacher.Listen : 订阅事件
            student.Test += teacher.Listen;

            string name = "Jeff";
            int id = 4;
            student.Talk(name,id);
        }
    }


    // 1.1 自定义事件信息
    // 自定义事件信息集成自 EventArgs
    // .net 规定传递事件信息需要 EventArgs 作为后缀
    public class TestEventArgs : EventArgs
    {
        public string name { get; set; }    
        public int id { get; set; }
    }

    // 1.2 声明委托
    // .net 规定委托如果是为了事件, 那么需要 EventHandler 作为后缀
    public delegate void TestEventHandler(Student student, TestEventArgs e);

    public class Student       
    {
        // 1.3 声明委托
        private TestEventHandler testEventHandler;

        // 1.4 声明事件
        public event TestEventHandler Test  
        {
            add
            {
                this.testEventHandler += value;
            }

            remove
            {
                this.testEventHandler -= value; 
            }
        }


        // 1.5 触发
        public void Talk(string name, int id)
        {
            // 判断事件是否被订阅
            if (this.testEventHandler != null)
            {
                // 添加事件信息
                TestEventArgs e = new TestEventArgs();
                e.id = id;
                e.name = name;
                // 通知订阅者
                Console.WriteLine("Student : Hello teacher, my name is " + name + " and my id is " + id);
                this.testEventHandler.Invoke(this,e);
            }
        }
    }

    class Teacher
    {
        // 1.6 事件处理器
        public void Listen(Student student, TestEventArgs e)
        {
            string res = "Teacher : Now i know " + "your id is " + e.id + " and your name is " + e.name;
            Console.WriteLine(res);
        }
    }
}

简略声明事件

using System;

namespace EventTest
{
    class EventDemo
    {
        static void Main(String[] args)
        {
            // student : 事件的拥有者
            Student student = new Student();
            // teacher : 事件的响应者
            Teacher teacher = new Teacher();
            // student.Test : 事件
            // teacher.Listen : 事件处理器
            // student.Test += Teacher.Listen : 订阅事件
            student.Test += teacher.Listen;

            string name = "Jeff";
            int id = 4;
            student.Talk(name,id);
        }
    }


    // 1.1 自定义事件信息
    // 自定义事件信息集成自 EventArgs
    // .net 规定传递事件信息需要 EventArgs 作为后缀
    public class TestEventArgs : EventArgs
    {
        public string name { get; set; }    
        public int id { get; set; }
    }

    // 1.2 声明委托
    // .net 规定委托如果是为了事件, 那么需要 EventHandler 作为后缀
    public delegate void TestEventHandler(Student student, TestEventArgs e);

    public class Student       
    {
        // 1.3 声明事件
        public event TestEventHandler Test;

        // 1.4 触发
        public void Talk(string name, int id)
        {
            // 判断事件是否被订阅
            if (this.Test!= null)
            {
                // 添加事件信息
                TestEventArgs e = new TestEventArgs();
                e.id = id;
                e.name = name;
                // 通知订阅者
                Console.WriteLine("Student : Hello teacher, my name is " + name + " and my id is " + id);
                this.Test.Invoke(this,e);
            }
        }
    }

    class Teacher
    {
        // 1.5 事件处理器
        public void Listen(Student student, TestEventArgs e)
        {
            string res = "Teacher : Now i know " + "your id is " + e.id + " and your name is " + e.name;
            Console.WriteLine(res);
        }
    }
}

使用厂商准备的事件 EventHandler

using System;

namespace EventTest
{
    class EventDemo
    {
        static void Main(String[] args)
        {
            // student : 事件的拥有者
            Student student = new Student();
            // teacher : 事件的响应者
            Teacher teacher = new Teacher();
            // student.Test : 事件
            // teacher.Listen : 事件处理器
            // student.Test += Teacher.Listen : 订阅事件
            student.Test += teacher.Listen;

            string name = "Jeff";
            int id = 4;
            student.Talk(name, id);
        }
    }


    // 1.1 自定义事件信息
    // 自定义事件信息集成自 EventArgs
    // .net 规定传递事件信息需要 EventArgs 作为后缀
    public class TestEventArgs : EventArgs
    {
        public string name { get; set; }
        public int id { get; set; }
    }

    public class Student
    {
        // 1.2 声明事件
        public event EventHandler Test;

        // 1.3 触发
        public void Talk(string name, int id)
        {
            // 判断事件是否被订阅
            if (this.Test != null)
            {
                // 添加事件信息
                TestEventArgs e = new TestEventArgs();
                e.id = id;
                e.name = name;
                // 通知订阅者
                Console.WriteLine("Student : Hello teacher, my name is " + name + " and my id is " + id);
                this.Test.Invoke(this, e);
            }
        }
    }

    class Teacher
    {
        // 1.4 事件处理器
        public void Listen(object sender, EventArgs e)  
        {
            TestEventArgs args = e as TestEventArgs;
            string res = "Teacher : Now i know " + "your id is " + args.id + " and your name is " + args.name;
            Console.WriteLine(res);
        }
    }
}

不同之处如下
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值