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
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.
Named delegate types can be declared using the delegate
keyword.
Delegates can be invoked using the same syntax as methods: the name of the delegate instance, followed by parentheses containing any parameters.
Delegates can be assigned to in the following ways:
delegate
keyword.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.