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

查看:136
本文介绍了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天全站免登陆