What does the event keyword do really?
event关键字的作用是什么?
msdn says it is used to declare an event in a publisher class
msdn表示它用于在发布者类中声明一个事件
// Declare the event.
public event SampleEventHandler SampleEvent;
Would that mean the SampleEvent is any less of a SampleEventHandler if I don't put event in front of it?
这是否意味着如果我不在它前面放置事件,SampleEvent就不再是SampleEventHandler?
Besides the super awesome -= and += operators what do I get out of events/eventhandlers that I wouldn't get from List<MyDelegate>? When should I use one over the other?
除了超级棒的 - =和+ =运算符,我从List
2 个解决方案
#2
A delegate supports += and -= operations, and so does an event. Also you can call them like methods.
委托支持+ =和 - =操作,事件也是如此。你也可以像方法一样调用它们。
But the difference is that calling an event is private to the class it belongs to, whereas += and -= are public. Also you can't assign to an event using = from outside the class either.
但不同的是,调用一个事件对它所属的类是私有的,而+ =和 - =是公共的。此外,您也无法使用来自课外的=来分配事件。
#1
See this other SO thread for a complete answer.
请参阅此其他SO主题以获得完整答案。
#2
A delegate supports += and -= operations, and so does an event. Also you can call them like methods.
委托支持+ =和 - =操作,事件也是如此。你也可以像方法一样调用它们。
But the difference is that calling an event is private to the class it belongs to, whereas += and -= are public. Also you can't assign to an event using = from outside the class either.
但不同的是,调用一个事件对它所属的类是私有的,而+ =和 - =是公共的。此外,您也无法使用来自课外的=来分配事件。