我应该使用EventHandler< T>和/或EventArgs委托模式?

编程入门 行业动态 更新时间:2024-10-28 00:23:17
本文介绍了我应该使用EventHandler< T>和/或EventArgs委托模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 阅读我的C#书,它谈到使用事件/代表(我假设我是正确的,认为一个事件相当于没有成员变量访问的公共代理)在MS中喜欢的模式:

public delegate Something(object o,EventArgs e)

然后再介绍一下关于 EventArgs< T> ,这基本上不需要委托声明:

public EventHandler< SomeEventArgs>事件

与(我想)相同的是/ b

private delegate Something(object o,SomeEventArgs e); 公共事件事件事件;

使用 EventHandler ?我可以看到为什么发送对象可能是有用的,但并不是所有的时间 - 而且很多时候,EventArgs可能会变得烦人的处理。 >

解决方案

Microsoft已经推出了一些使C#工作愉快的好方式。话虽如此,我建议你写你的事件处理程序,以方便你的客户端代码,而不是写很多代码,只是为了满足一个模式。

delegate void ClientMessageHandler(IClient client,IMessage message);

Reading my C# book, it talks about using events/delegates (I am assuming I am right in thinking an event is the equivalent of a public delegate which has no member variable access) in a pattern liked by MS:

public delegate Something(object o, EventArgs e)

And then goes onto explain about EventArgs<T> which basically removes the need for the delegate declaration:

public EventHandler<SomeEventArgs> events

Which is the same as (I think)

private delegate Something(object o, SomeEventArgs e); public event Something events;

Is it a good idea to use EventHandler? I can see why sending the object could be useful, but not all the time - and a lot of the time, the EventArgs may just become annoying to deal with.

解决方案

Microsoft has definitely pushed some great patterns that has made working with C# a pleasant experience. That being said, I recommend you write your event handlers to be convenient for your client code rather than writing a lot of code just to meet a pattern.

delegate void ClientMessageHandler(IClient client, IMessage message);

更多推荐

我应该使用EventHandler&lt; T&gt;和/或EventArgs委托模式?

本文发布于:2023-11-07 06:58:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   amp   EventHandler   lt   EventArgs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!