C#委托事件随笔

本文通过一个银行取款并通知用户的例子介绍了C#中事件与委托的应用。用户实例化后注册了银行的发送通知事件,当执行取款操作时触发事件,用户会收到短信和邮件通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 //用户去银行取钱(发布方),用户收到短信提醒,(接收方)
    class Program
    {
        static void Main(string[] args)
        {
            Bank b = new Bank();                      //实例化银行
            User xm = new User("110","1278326@qq.com");         //实例化用户 
            b.send += new Bank.sendEventHandle(xm.ReviceTel);     //注册事件
            b.send += new Bank.sendEventHandle(xm.ReviceEmail);    //注册事件
            b.issure(xm);                           //执行事件
            Console.ReadKey();
        }
    }

    public class Bank {

        public delegate void sendEventHandle(object o, User xx);  //定义委托

        public event sendEventHandle send;              //基于委托的事件

        public void issure(User xx) {
            send(this,xx);
        }
    }

    public class User:EventArgs{

        public string tel;
        public string email;

        public User(string telephone, string email)
        {
            this.email = email;
            this.tel = telephone;
        }

        public void ReviceTel(object o , User x)
        {
            Console.WriteLine("收到短信"+ x.tel);

        }

        public void ReviceEmail(object o, User x)
        {
            Console.WriteLine("收到emile" + x.tel);

        }

    }

 

转载于:https://www.cnblogs.com/YorkZhao/p/7800803.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值