50

There are some Delegates predefined in C#

I know these:

EventHandler // Default event callbacks
EventHandler<T> // Default event callbacks with custom parameter (inheriting from EventArgs)
Action // Function without return value and without parameter
Action<T1, T2, T3, T4> // Function without return value and 1-4 parameters
Func<T1, T2, T3, T4, TResult> // Methos with 0-4 parameters and one result type
Predicate<T> // equivalent to Func<T, bool>

There are many more for special cases and generated form parts of the framework, but these are often good to use in self written code.

If you know some more useful add them. Otherwise this is answered.

Tarion
  • 16,283
  • 13
  • 71
  • 107

6 Answers6

29

They're not predefined in C#. They're defined by the framework.

The Action and Func delegate families are wider than you've shown - they go up to

Action<T1, T2, T3, T4>

and

Func<T1, T2, T3, T4, TResult>

Another common-ish one in .NET 2.0 for list manipulation (before LINQ) is Predicate<T>.

For working with threads:

ThreadStart
ParameterizedThreadStart
WaitCallback
TimerCallback
AsyncCallback
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • It does make you wonder though. If generics and Func / Action were available in 1.0, would we have any other delegate types? – JaredPar Apr 08 '09 at 13:19
  • It's an interesting question. I rather like Predicate as a more meaningful name than Func for instance - if it weren't for the overloads of Where etc to take Func it would be a better fit for LINQ. Specifying type arguments everywhere can hurt readability. – Jon Skeet Apr 08 '09 at 13:44
5

I like to use Predicate<T> which is equivalent to Func<T, bool>

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
2

Goto Reflector Search for System.MulticastDelegate and check the derived types. You will get list of all the Delegates you are looking for.

NileshChauhan
  • 5,469
  • 2
  • 29
  • 43
  • Well, that gives a complete list of delegates. It doesn't give the most useful or important ones, which is what the OP was after I think. – Jon Skeet Apr 08 '09 at 13:13
2

I've also come across

Comparison<T> 

in the

List.Sort(Comparison<T>)

generic method. I suppose this is now equivalent to:

Func<T1, T2, int> 
0

I use WaitCallback and ThreadStart often enough for them to get a mention.

If you know the signature of the delegate you're after, but you don't know if there's an existing delegate with that signature that you can use, you can follow these instructions on my blog to find one.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

System.Windows.ValidateValueCallback which represents a method used as a callback that validates the effective value of a dependency property.

Read More: MSDN: ValidateValueCallback Delegate

M. Jahedbozorgan
  • 6,914
  • 2
  • 46
  • 51