前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c#之委托和事件

c#之委托和事件

作者头像
liulun
发布2022-05-09 10:56:05
2440
发布2022-05-09 10:56:05
举报
文章被收录于专栏:liulun

一:委托简介 委托是一种指针,保持方法的引用 委托使得方法可以作为参数进行传递 如:

代码语言:javascript
复制
    public delegate void MyDelegate();
    class Program
    {
        private static void SayHellow()
        {
            Console.WriteLine("Hellow world");
        }
        static void Main(string[] args)
        {
            //MyDelegate a = new MyDelegate(SayHellow);//也可以
            MyDelegate a = SayHellow;
            a();
            Console.ReadKey();
        }
    }

委托其实是一种特殊的类 他定义了方法的签名 使得与该签名相同的方法都能被此委托所引用 有了这个特性就可以把方法当作参数进行传递

二:匿名方法 委托可以不用已存在的方法创建 如

代码语言:javascript
复制
    public delegate void MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate a = delegate()
            {
                Console.WriteLine("HellowWorld");
            };
            a();
            Console.ReadKey();
        }
    }

匿名方法在lambda表达式出来之后,更见锋芒,以后再说 委托可以绑定匿名方法,实例方法和静态方法

三:多播委托 委托支持操作符重载 可以将多个方法绑定到同一个委托 也可以从一个委托移除某一方法 可以把一个方法绑定多次,运行时也会执行多次 如

代码语言:javascript
复制
    public delegate void MyDelegate();
    class Program
    {
        private static void SayHellow()
        {
            Console.WriteLine("Hellow world");
        }
        private static void SayHellow2()
        {
            Console.WriteLine("Hey world");
        }
        static void Main(string[] args)
        {
            MyDelegate a = SayHellow;
            a += SayHellow2;
            a();
            Console.ReadKey();
        }
    }

一个委托实例指向多个方法,这些方法是无序的,设计时不要依赖这种顺序

四:事件 可以不用初始化事件就直接用+=操作符绑定方法 观察者模型(此方法JimmyZiYang原创,此处做了适当修改,在此表示感谢)

代码语言:javascript
复制
 public delegate void BoiledEventHandler(object sender,BoliedEventArgs e);
    
    public class BoliedEventArgs : EventArgs
    {
        public readonly int temperature;
        public BoliedEventArgs(int temperature)
        {
            this.temperature = temperature;
        }
    }

    public class Heater 
    {
        private int temprature;
        public string type = "RealFire 001";
        public string area = "HangZhou China";
        public event BoiledEventHandler Boiled;
        protected virtual void OnBolied(BoliedEventArgs e)
        {
            if (Boiled != null)
            { Boiled(this, e); }
        }
        public void BoilWater()
        {
            for (int i = 0; i < 100; i++)
            {
                temprature = i;
                if (temprature > 95)
                {
                    BoliedEventArgs e = new BoliedEventArgs(temprature);
                    OnBolied(e);
                }
            }
        }
    }
    public class Alarm
    {
        public void MakeAlert(object Sender, BoliedEventArgs e)
        {
            Heater heater = (Heater)Sender;
            Console.WriteLine("Alarm:{0}-{1}", heater.area, heater.type);
            Console.WriteLine("Alarm:水已经到{0}度了", e.temperature);
            Console.WriteLine();
        }
    }
    public class Display
    {
        public static void ShowMsg(object sender, BoliedEventArgs e)
        {
            Heater heater = (Heater)sender;
            Console.WriteLine("display:{0}-{1}", heater.area, heater.type);
            Console.WriteLine("display:水快烧开了,当前温度{0}", e.temperature);
            Console.WriteLine();
        }
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            Heater heater = new Heater();
            Alarm alarm = new Alarm();
            heater.Boiled += alarm.MakeAlert;
            heater.Boiled += Display.ShowMsg;

            heater.BoilWater();
            Console.ReadKey();
        }
    }

输出结果

image
image

本文编写过程中得到了  钧梓昊逑  的帮助,在此表示感谢!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2009-05-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档