George Mauer
George Mauer

Reputation: 122172

Anyone know of a list of delegates already built into the framework?

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:

and so on

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

Upvotes: 6

Views: 2775

Answers (5)

Mike Dimmick
Mike Dimmick

Reputation: 9802

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.

Upvotes: 0

Yes - that Jake.
Yes - that Jake.

Reputation: 17119

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.

Upvotes: 2

Thomas Danecker
Thomas Danecker

Reputation: 4685

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.

Upvotes: 1

Jorge C&#243;rdoba
Jorge C&#243;rdoba

Reputation: 52123

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

Here you got a direct link: List of delegates

That should get you started.

Upvotes: 7

Kent Boogaart
Kent Boogaart

Reputation: 178770

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

Upvotes: 3

Related Questions