委托和事件温习

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

namespace TestEvent
{
    class MyClass
    {
        public delegate void CompletedEventHandler();

        public event CompletedEventHandler WorkCompleted;


        public void Fire()
        {
            if (this.WorkCompleted != null)
            {
                this.WorkCompleted();
            }

         
        }
    }
    class Program
    {
        static void TestEvent()
        {
            Console.WriteLine("test event");
        }

        static void TestEvent2()
        {
            Console.WriteLine("test event2");
        }

        static void TestEvent3()
        {
            Console.WriteLine("test event3");
        }
        static void TestDelegate()
        {
            Console.WriteLine("test delegate");
        }

        static void Main(string[] args)
        {

            MyClass myObject = new MyClass();
            myObject.WorkCompleted += TestEvent;
            myObject.WorkCompleted += TestEvent2;
            myObject.WorkCompleted += TestEvent3;
            myObject.WorkCompleted -= TestEvent3;
            myObject.Fire();
            Console.ReadLine();
        }
    }
}

 

posted @ 2016-12-19 15:58  甜菜波波  阅读(130)  评论(0编辑  收藏  举报