c#之委托和事件

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

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

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

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

    public delegate void MyDelegate();
    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate a = delegate()
            {
                Console.WriteLine("HellowWorld");
            };
            a();
            Console.ReadKey();
        }
    }

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

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

    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原创,此处做了适当修改,在此表示感谢)

 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

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

posted @   liulun  阅读(886)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示