返回   cpper编程论坛 > Blog > cat
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

为这篇文章评分

C# events VS delegates

发表于 2006-11-17 03:09 PM 作者: cat
C# events and delegates look asame at first glance. They both support operator +=, operator -=, invoking like a method, etc. However there should be some difference.

I searched it on the web and got this article:
http://blog.monstuff.com/archives/000040.html
And I summarize the major difference here:
1. events can be declared as a part of interface, while a delegate field can’t.
2. events have to be invoked within the class that declares it, while delegates are not restricted.
3. There is one thing called event accessors, which I’d never known before.
代码:
public delegate void MsgHandler(string msg);
class TestClass : ITest
{
  public event MsgHandler msgNotifier 
  {
    add 
    { 
      Console.WriteLine("hello");
      msgNotifier += value; 
    }
  }
}
4. the .NET fx adds a restriction on the signature of events. They should be foo(object source, EventArgs e). However C# allows any delegate as an event.
评论 0 Email文章
评论总数 0

评论

发表评论 发表评论
作者为 cat 的最新文章

所有时间均为格林尼治时间 +9。现在的时间是 10:18 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0