c#事件(2)之相关内容

来源:互联网 发布:nginx ab压力测试工具 编辑:程序博客网 时间:2024/06/08 09:15
//在Interface(接口上实现Event)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//接口实现:事件是基于委托的
namespace Event
{
    class Program
    {
        static void Main(string[] args)
        {
            I i = new MyClass();
            i.MyEvent += new MyDelegate(f);
            i.FireAway();

            Console.ReadLine();
        }

        private static void f()
        {
            Console.WriteLine("This is called where the event fired");
        }

    }

    public delegate void MyDelegate();
    public interface I
    {
        event MyDelegate MyEvent;
        void FireAway();
    }

    public class MyClass:I
    {
        public event MyDelegate MyEvent;

        public void FireAway()
        {
            if (MyEvent != null)
                MyEvent();
        }
    }

}

0 0
原创粉丝点击