C#事件与接口

using System;

namespace ConsoleApplication1d
{
    delegate void MsgDel(string s);
    interface IMsg
    {
        event MsgDel msgd;
        void Excute(string s);
    }

    class MInfo : IMsg//必须实现接口的全部成员,如事件,函数
    {
        //不写这句会提示 Minfo does not implement interface memeber 'IMsg.msgd'
        public event MsgDel msgd; //这是对接口中事件的实现!,这里所谓的【实现】比较诡异, 仅仅是重新声明一次
        public void Excute(string s) //对接口中Excute的实现
        {
            if (null != msgd)
                msgd(s);
        }
    }
    class Test
    {
        public static void Main()
        {
            IMsg msg = new MInfo();
            msg.msgd += MSGA;
            msg.msgd += MSGB;

            msg.Excute("hello");
            //output
            // MSGA: hello 
            // MSGB: hello
        }

        public static void MSGA(string s)
        {
            Console.WriteLine("MSGA: " + s);
        }

        public static void MSGB(string s)
        {
            Console.WriteLine("MSGB: " + s);
        }
    }
}
原文地址:https://www.cnblogs.com/timeObjserver/p/5966629.html