C#_delegate

//public event SecondChangeHandler OnSecondChange; 若将委托加上event,则视作是事件,不是委托,外围就不能直接对OnSecondChange传值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace EventClock
{
    //一个存放事件信息的类
    //存放这Clock类中的信息,和其他状态信息
    public class TimeInfoEventArg : EventArgs
    {
        public TimeInfoEventArg(int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
        public readonly int hour;
        public readonly int minute;
        public readonly int second;
    }

    //中心类:其他类要订阅的类
    //发布一个委托
    //OnSecondChange
    public class Clock
    {
        private int hour;
        private int minute;
        private int second;
        //订阅这必须实现的委托
        public delegate void SecondChangeHandler(object clcok, TimeInfoEventArg timeInformation);
        //委托实例
        public SecondChangeHandler OnSecondChange;

        //设置始终运行,每一秒都触发一个事件
        public void Run()
        {
            for (; ; )
            {
                //休眠10秒
                Thread.Sleep(10);
                //获取当前时间
                System.DateTime dt = System.DateTime.Now;
                //如秒钟改变,通知订阅者
                if (dt.Second != second)
                {
                    //创建TimeInfoEventArg对象,传给订阅者
                    TimeInfoEventArg timeInformation = new TimeInfoEventArg(dt.Hour,dt.Minute,dt.Second);
                    //如果有订阅者,通知他们
                    if (OnSecondChange != null)
                    {
                        OnSecondChange(this, timeInformation);
                    }

                }
                this.second = dt.Second;
                this.minute = dt.Minute;
                this.hour = dt.Hour;
            }
        }
    }
    //一个订阅者,DisplayClock订阅
    //时钟事件,DisplayClock任务是显示当前时间
    public class DisplayClock
    {
        //对于给定的时钟,订阅其他OnSecondChange事件
        public void Subscribe(Clock theClock)
        {
            theClock.OnSecondChange += new Clock.SecondChangeHandler(TimeHasChanged);

        }

        //实现委托功能的方法
        public void TimeHasChanged(object clcok, TimeInfoEventArg timeInformation)
        {
            Console.WriteLine("Current time: {0}:{1}:{2}", 
                timeInformation.hour.ToString(),
                timeInformation.minute.ToString(),
                timeInformation.second.ToString());
        }
    }

    //另一个订阅者,其任务写入文件
    public class LogCurrentTime
    {
        public void Subscribe(Clock theClock)
        {
            theClock.OnSecondChange += new Clock.SecondChangeHandler(WriteLogEntry);
        }
        //此方法应该写入文件
        //写到控制台是为了看到对象不保存状态的效果
        public void WriteLogEntry(
            object theClcok, TimeInfoEventArg ti)
        {
            Console.WriteLine("logging to file: {0}:{1}:{2}",
                ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString());
        }
    }

 
    

    class Program
    {
        static void Main(string[] args)
        {
            Clock theClock = new Clock();

            //创建时钟对象,订阅刚刚创建的时钟
            DisplayClock dc = new DisplayClock();
            dc.Subscribe(theClock);
            //创建Log对对象
            LogCurrentTime lct = new LogCurrentTime();
            lct.Subscribe(theClock);

            theClock.Run();
            Console.ReadLine();
        }
    }
}


//匿名方法,无需在注册实例委托
        public void Subscribe(Clock theClock)
        {
            theClock.OnSecondChange += delegate(object clcok, TimeInfoEventArg timeInformation)
            {
                Console.WriteLine("Current time: {0}:{1}:{2}",
                timeInformation.hour.ToString(),
                timeInformation.minute.ToString(),
                timeInformation.second.ToString());
            };

        }


原文地址:https://www.cnblogs.com/MarchThree/p/3720445.html