简体   繁体   中英

Should I use EventHandler<T> and/or the EventArgs delegate pattern?

Reading my C# book, it talks about using events/delegates (I am assuming I am right in thinking an event is the equivalent of a public delegate which has no member variable access) in a pattern liked by MS:

public delegate Something(object o, EventArgs e)

And then goes onto explain about EventArgs<T> which basically removes the need for the delegate declaration:

public EventHandler<SomeEventArgs> events

Which is the same as (I think)

private delegate Something(object o, SomeEventArgs e);

public event Something events;

Is it a good idea to use EventHandler ? I can see why sending the object could be useful, but not all the time - and a lot of the time, the EventArgs may just become annoying to deal with.

Microsoft has definitely pushed some great patterns that has made working with C# a pleasant experience. That being said, I recommend you write your event handlers to be convenient for your client code rather than writing a lot of code just to meet a pattern.

delegate void ClientMessageHandler(IClient client, IMessage message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM