C#:应该在哪里放置事件处理程序委托?

发布于 2024-07-17 07:32:29 字数 647 浏览 7 评论 0原文

我有这个类:

public class GenericEventArgs<T> : EventArgs
{
    public GenericEventArgs() : this(default(T)) {}
    public GenericEventArgs(T value) { Value = value; }
    public T Value { get; private set; }
}

以及它的事件处理程序委托:

public delegate void GenericEventHandler<T>(object sender, GenericEventArgs<T> e);

我目前将它们放在同一个文件中的命名空间中。 这被认为是坏的/混乱的/等等吗? 因为,一般来说,我会说每个文件应该只包含一个类。 因此,为了使其干净,我更愿意在文件中单独包含 GenericEventArgs 类。 但后来我有了这个 GenericEventHandler 委托,我不确定应该将其放置在哪里。 它应该有自己的文件吗? 只是……那一行? (当然还有命名空间)

你通常如何做到这一点?

I have this class:

public class GenericEventArgs<T> : EventArgs
{
    public GenericEventArgs() : this(default(T)) {}
    public GenericEventArgs(T value) { Value = value; }
    public T Value { get; private set; }
}

And this event handler delegate for it:

public delegate void GenericEventHandler<T>(object sender, GenericEventArgs<T> e);

I currently have these in the same file together in a namespace. Is that considered bad/messy/etc.? Cause, in general I would say that each file should contain only one class. So to have it clean I would prefer to have the GenericEventArgs class in the file alone. But then I have this GenericEventHandler<T> delegate that I am not sure where I should place. Should it have its own file? With just... that one line kind of? (and the namespace of course)

How do you usually do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聊慰 2024-07-24 07:32:29

有什么理由不使用 EventHandler? 我以为有一个等效的 EventArgs 但我现在看不到它......无论如何,我将 GenericEventArgs 放在 GenericEventArgs 中。就我

个人而言,当我想引入自己的委托类型(说实话,这种情况越来越少)时,我会创建一个 Delegates.cs 文件,其中包含名称空间的所有适当委托。然后我就知道在哪里可以找到它们,而无需单个声明的文件。

Any reason for not using EventHandler<TEventArgs>? I thought there was an equivalent EventArgs<T> but I can't see it at the moment... Anyway, I'd put GenericEventArgs in GenericEventArgs.cs

Personally, when I want to introduce my own delegate types (which is increasingly rare, to be honest) I create a Delegates.cs file with all the appropriate delegates for the namespace in. Then I know where to find them without having a file for a single declaration.

旧时模样 2024-07-24 07:32:29

当我想创建自己的事件处理程序委托时,我曾经创建一个文件,该文件具有我在自己的事件处理程序委托中使用的 EventArgs 类的名称。
例如“MyEventArgs.cs”。

该文件包含 MyEventArgs 类以及使用该类的 MyEventHandler 委托。

然而现在,我使用现有的通用 EventHandler (EventHandler),并且只需创建自定义 EventArgs 类。 所以,这个‘问题’对我来说已经不存在了。 :)

When I wanted to create my own eventhandler delegates, I used to create one file which had the name of the EventArgs class that I used in my own eventhandler delegate.
For instance 'MyEventArgs.cs'.

That file contained the MyEventArgs class, and the MyEventHandler delegate that used this class.

Now however, I use the existing generic EventHandler (EventHandler<T>), and I only have to create my custom EventArgs class. So, this 'problem' doesn't exists anymore for me. :)

鲸落 2024-07-24 07:32:29

好吧,我会将 GenericEventArgs 类放入其自己的名为

GenericEventArgs_T.cs 的文件中,与其他所有内容位于同一命名空间中。

我会将您的委托(以及事件,如果有的话)放在将要公开该事件的类中。

Well I'd put the GenericEventArgs<T> class in it's own file called

GenericEventArgs_T.cs in the same namespace as everything else.

And I'd put your delegate (and event if there is one) in the class that's going to have that event exposed on it.

蒗幽 2024-07-24 07:32:29

我会做你已经在做的事情,然后将所有不用于事件的委托放在一个名为 Delegates.cs 或类似文件的单独 .cs 文件中。

I'd do what you are already doing and then put all delegates that are not used for events in a separate .cs file called Delegates.cs or something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文