Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Where should you place event handler delegates?

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?

like image 731
Svish Avatar asked Dec 17 '22 08:12

Svish


1 Answers

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.

like image 117
Jon Skeet Avatar answered Jan 03 '23 05:01

Jon Skeet