前段参加了个面试,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);
}
}
}
最终,输出结果为
111
222
看来,两个事件的叠加,效果也是叠加的!
本文转自Jack Niu博客园博客,原文链接:http://www.cnblogs.com/skywind/archive/2007/04/13/712169.html,如需转载请自行联系原作者