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.
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.
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;
}
}
} 评论总数 0
评论
发表评论 |
作者为 cat 的最新文章
- C# events VS delegates (2006-11-17)
- .net 匿名delegate 可以修改的局部变量 (2006-11-17)
- 做个简单的黑白棋(2) (2006-02-04)
- 做个简单的黑白棋(1) (2006-02-02)
- Stored Procedure (2006-01-28)




