tags:

views:

581

answers:

5

I find myself writing delegates occasionally for really simple functions (take no arguments and return void for example) and am wondering if anyone knows someplace that has compiled a list of all the predefined delegates already available in the .NET framework so I can reuse them?

To be clear I am looking for something like this:

  • void System.AsyncCallback(System.IAsyncResult)
  • int System.Comparison(T x, T y)
  • void System.IO.ErrorEventHandler(object, System.Io.ErrorEventArgs)

and so on

If not, sounds like a good idea for a blog article.

+2  A: 

I have previously blogged along these lines here. Basically, I describe how you can find an existing delegate to meet your needs using Reflector.

Kent Boogaart
+2  A: 

Just look in the msdn database for (T) delegate.

Here you got a direct link: List of delegates

That should get you started.

Jorge Córdoba
and a thanks to you
George Mauer
+1  A: 

Just use the Action, Action<T>, Action<T1,T2,..> delegates for methods not returning anything (void), or the Func<TResult>, Func<T, TResult>, Func<T1, ..., TResult> delegates for methods returning TResult.

Those delegates are new in .net 3.5.

Thomas Danecker
+1  A: 

One thing to keep in mind is that you write code to be readable to future coders, including your future self. Even if you can find a built-in delegate with the correct signature in the framework, it's not always correct to use that delegate if it obscures the purpose of the code.

Six months down the road, the use of a delegate of type BondMaturationAction is going to be much clearer than that of one with a type Action, even if the signatures are the same.

Jekke
A: 

In .NET 2.0 and later, use EventHandler if you have no arguments at all, and EventHandler<T> if you want to provide some custom data (you will need to derive a class from EventArgs with your additional data in it). If you have no EventArgs to use, pass EventArgs.Empty.

Because EventArgs is a reference type, all instances of EventHandler<T> use the same JITted code.

Mike Dimmick