views:

739

answers:

1

Hello

Just a quick one: what are peoples thoughts on using Action delegates for public class events vs defining ones own event delegate types? I know many use Actions for "minor" delegates such as in lamdas and .ForEach() extension methods, etc, but for actual class event members, is using Actions a good idea? What is "best practice" in this area.

Thanks

+7  A: 

Instead of Action, I use EventHandler<TEventArgs> for any event declarations. It removes the need for me to define my own delegate type. It also has the additional benefit of forcing the data type to be derived from System.EventArgs and hence plays nicely with .Net event patterns.

JaredPar
But it does force you to roll up an implementation of TEventArgs, even if you just want to return one value... Having said which, a [ScalarEventArgs<T> where T: struct] would suffice.
Benjol
Is conforming to the .NET event model really worth the effort of creating a container class that inherits from System.EventArgs? What I am I really losing by not conforming to this model?
MrLane
One thing that can happen is if you need to later on return some extra info with your event all you have to do is add it to your EventArgs class. Where as if you return say Action<String> you always have to return a string no matter what, and if you change it, it will break all your code that uses it.
Nathan W