Akann的博客 发表于 2013-1-2 23:06:38

c#中的event关键字

<div id="cnblogs_post_body">这段代码取自stackoverflow:
   
<div class="cnblogs_code">using System;class Akann{    static void Main(string[] args)    {      var a = new A();      a.eventOne += (s, e) => Console.WriteLine("One");      a.eventTwo += (s, e) => Console.WriteLine("Two");      a.RaiseOne();      a.RaiseTwo();      a.eventOne(null, EventArgs.Empty);//这句将不能被编译      a.eventTwo(null, EventArgs.Empty);    }}class A {    public event EventHandler eventOne;    public EventHandler eventTwo;    public void RaiseOne()    {      if (eventOne != null)            eventOne(this, EventArgs.Empty);    }    public void RaiseTwo()    {      if (eventTwo != null)            eventTwo(this, EventArgs.Empty);    }}
页: [1]
查看完整版本: c#中的event关键字