前段参加了个面试,C#中,两个事件叠加,如下
t.EventTest += delegate { Console.WriteLine("111"); };
t.EventTest += delegate { Console.WriteLine("222"); };其输出结果会是什么样的? 还是测试一下吧:
using System;
using System.Collections.Generic;using System.Text;namespace ConsoleApplication1
{ class Program{ static void Main(string[] args){ Test1 t = new Test1();t.EventTest += delegate { Console.WriteLine("111"); };t.EventTest += delegate { Console.WriteLine("222"); };t.StarEvent();
}}class Test1{ public event EventHandler EventTest;public void StarEvent(){ if (this.EventTest != null)this.EventTest(this, null);}}}最终,输出结果为
111222看来,两个事件的叠加,效果也是叠加的!