首页 > 代码库 > 委托和事件的整理

委托和事件的整理

  public class UploadEnventcs
    {
        public event EventHandler CreateEvent;
        public delegate void CreateDele(object sender);
        public CreateDele cd;

        public void Upload()
        {
            if (CreateEvent != null)
            {
                Console.WriteLine("CreateEvent绑定的有事件");
                CreateEvent(this, null);
            }
            if (cd != null)
            {
                Console.WriteLine("CreateDele绑定的有事件");
                cd(this);
            }
        }
    }

调用

   class Program
    {
        static void Main(string[] args)
        {
            UploadEnventcs ue = new UploadEnventcs();
            ue.CreateEvent += ue_CreateEvent;
            ue.cd = new UploadEnventcs.CreateDele(CreateDelegate);

            ue.Upload();
        }

        static void ue_CreateEvent(object sender, EventArgs e)
        {
            Console.WriteLine("已执行ue_CreateEvent");
        }
        public static void CreateDelegate(object sender)
        {
            Console.WriteLine("已执行CreateDele委托");
        }
    }