/*
*
* 事件不能声明局部实例
* 事件不能作为方法的参数
*
*
* ***/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A13_DifferentBetweenDelegateAndEvent
{
class Demo1
{
public Action actHandler; //委托
public event Action eveEvent; //事件
public Demo1()
{
//委托注册
actHandler += TestMethod1;
actHandler += TestMethod2;
//事件注册
eveEvent += TestMethod1;
eveEvent += TestMethod2;
}
public void TestMethod1()
{
Console.WriteLine("TestMethod1");
}
public void TestMethod2()
{
Console.WriteLine("TestMethod2");
}
public void Test1()
{
if (actHandler!=null)
{
actHandler();
}
if (eveEvent != null)
{
eveEvent();
}
}
public void Test2()
{
//定义委托的局部实例
Action actHandler2 = TestMethod1;
actHandler2 += TestMethod2;
}
static void Main(string[] args)
{
Demo1 obj = new Demo1();
obj.Test1();
}
}
}