C#中的事件数组? [英] array of events in C#?

查看:58
本文介绍了C#中的事件数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上:

public delegate void RecvCommandHandler (ChatApplication sender, byte[] content);
event RecvCommandHandler[] commands = new RecvCommandHandler[255];

我想为每个命令编号激活不同的方法/函数,但我真的不确定语法.我该怎么做?

I want to activate a different method/function for each command number, but I am really uncertain of the syntax. How am I supposed to do it?

对于这个问题,我想我只会用一组代表,但这个问题仍然很有趣.

I think I'll go with just an array of delegates for this one, but the question is still interesting.

推荐答案

实际上没有事件数组的概念 - 就像谈论属性数组一样.事件实际上只是让您订阅和取消订阅处理程序的方法.如果您需要能够通过索引来做到这一点,我建议您只需要一对方法.(AddCommandHandler(int, RecvCommandHandler)RemoveCommandHandler(int, RecvCommandHandler)).当然,这不会支持正常的事件处理语法糖,但我认为没有很多替代方案.

There's really no concept of an array of events - it's like talking about an array of properties. Events are really just methods which let you subscribe and unsubscribe handlers. If you need to be able to do this by index, I suggest you just have a pair of methods. (AddCommandHandler(int, RecvCommandHandler) and RemoveCommandHandler(int, RecvCommandHandler)). That won't support the normal event handling syntactic sugar, of course, but I don't see that there's a lot of alternative.

这篇关于C#中的事件数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆