Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard delegates in C# [closed]

Tags:

c#

list

delegates

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.

like image 957
Tarion Avatar asked Apr 08 '09 13:04

Tarion


People also ask

What are the default delegates in C#?

Func, Action and Predicate are generic inbuilt delegates present in System namespace. All three can be used with method, anonymous method and lambda expression.

What is delegate with example?

Delegates allow methods to be passed as parameters. Delegates can be used to define callback methods. Delegates can be chained together; for example, multiple methods can be called on a single event. Methods don't have to match the delegate type exactly.

What is a delegate type?

A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Delegates are especially used for implementing events and the call-back methods.

What is a generic delegate?

A delegate can define its own type parameters. Code that references the generic delegate can specify the type argument to create a closed constructed type, just like when instantiating a generic class or calling a generic method, as shown in the following example: C# Copy.


1 Answers

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 
like image 71
Jon Skeet Avatar answered Sep 19 '22 18:09

Jon Skeet