Should I use EventHandler and/or the EventArgs

2019-04-06 03:36发布

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.

1条回答
Animai°情兽
2楼-- · 2019-04-06 03:53

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);
查看更多
登录 后发表回答