Delegates

suggest change

Remarks

Summary

A delegate type is a type representing a particular method signature. An instance of this type refers to a particular method with a matching signature. A method parameter may have a delegate type, so one method can be passed a reference to another method and then invoke it.

In-built delegate types: Action<...>, Predicate<T> and Func<...,TResult>

The System namespace contains Action<...>,Predicate<T> and Func<...,TResult> delegates, where the “…” represents between 0 and 16 generic type parameters (for 0 parameters, Action is non-generic).

Func represents methods with a return type matching TResult, and Action represents methods without a return value (void). In both cases, the additional generic type parameters match, in order, the method parameters.

Predicate<T> represents a method that takes a T and returns bool.

Custom delegate types

Named delegate types can be declared using the delegate keyword.

Invoking delegates

Delegates can be invoked using the same syntax as methods: the name of the delegate instance, followed by parentheses containing any parameters.

Assigning to delegates

Delegates can be assigned to in the following ways:

Combining delegates

Multiple delegate objects can be combined with the + operator. The - operator removes a component delegate from another delegate.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents