什么是声明在C#中的事件的语法? [英] What is the syntax to declare an event in C#?
本文介绍了什么是声明在C#中的事件的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的课堂我要声明的事件其他类可以订阅。什么是声明事件的正确方法
这不工作:
公共事件CollectMapsReportingComplete;
解决方案
您忘了提到在类型。对于真正的简单事件, 事件处理程序
可能就足够了:
公共事件的EventHandler CollectMapsReportingComplete;
有时你会想声明自己的委托类型用于您的活动,让您在使用为 EventArgs的
参数自定义类型(见亚当·罗宾逊的评论):
公共委托无效CollectEventHandler(对象源,MapEventArgs参数);
公共类MapEventArgs:EventArgs的
{
公开的IEnumerable<地图>地图{搞定;组; }
}
您也可以使用通用的 事件处理程序
类型,而不是声明自己的类型:
公共事件的EventHandler< MapEventArgs> CollectMapsReportingComplete;
In my class I want to declare an event that other classes can subscribe to. What is the correct way to declare the event?
This doesn't work:
public event CollectMapsReportingComplete;
解决方案
You forgot to mention the type. For really simple events, EventHandler
might be enough:
public event EventHandler CollectMapsReportingComplete;
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; }
}
You can also use the generic EventHandler
type instead of declaring your own types:
public event EventHandler<MapEventArgs> CollectMapsReportingComplete;
这篇关于什么是声明在C#中的事件的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文