在C#中声明事件的语法是什么? [英] What is the syntax to declare an event in C#?
本文介绍了在C#中声明事件的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的类中,我想声明一个其他类可以订阅的事件。申报活动的正确方法是什么?
In my class I want to declare an event that other classes can subscribe to. What is the correct way to declare the event?
这不行:
public event CollectMapsReportingComplete;
推荐答案
你忘了提到 STRONG>。对于非常简单的事件, EventHandler
可能足够了:
You forgot to mention the type. For really simple events, EventHandler
might be enough:
public event EventHandler CollectMapsReportingComplete;
有时您将要声明自己的委托类型用于您的活动,从而允许您使用一个用于 EventArgs
参数的自定义类型(请参阅Adam Robinson的评论):
Sometimes you will want to declare your own delegate type to be used for your events, allowing you to use a custom type for the EventArgs
parameter (see Adam Robinson's comment):
public delegate void CollectEventHandler(object source, MapEventArgs args);
public class MapEventArgs : EventArgs
{
public IEnumerable<Map> Maps { get; set; }
}
您还可以使用通用的 EventHandler
类型,而不是声明自己的类型: p>
You can also use the generic EventHandler
type instead of declaring your own types:
public event EventHandler<MapEventArgs> CollectMapsReportingComplete;
这篇关于在C#中声明事件的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文