基本上:
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#中的事件数组?
发布评论