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. Method parameters may have delegate types, and so this one method to be passed a reference to another method, which may then be invoked

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 represents method with boolean return type, T is input parameter.

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 assigned to one delegate instance by using the \+ operator. The \- operator can be used to remove 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