Delegates in Fore.NET programs enable the user to implement scenarios that in other languages are implemented using function pointers. Unlike function pointers, delegates are fully object oriented, and encapsulate both an object reference and its method.
A delegate declaration describes a class that is derived from the System.Delegate class. A delegate instance encapsulates an invocation list, which is a list one or more methods, each of which is referred to as a callable entity. For instance methods, a callable entity consists of a reference to an instance and its method. For static methods, a callable entity consists of just a method. Invoking a delegate with an appropriate set of arguments causes each of the delegate's callable entities methods to be invoked with the given set of arguments one after another.
Delegates are declared as follows:
delegate-declaration:
attributesopt delegate-modifiersopt Delegate identifier
formal-parameter-listopt delegate-return-typeopt ;
delegate-return-type:
: return-type
delegate-modifiers:
delegate-modifier
delegate-modifiers delegate-modifier
delegate-modifier:
Public
Friend
It is a compile error for the same modifier to appear multiple times in a delegate declaration.
A delegate and a method are compatible if both the following conditions are true:
They have the same number of parameters of the same types determined in the same order with the same parameter modifiers.
They have the same return type.
The only method of delegate declaration is using the language construction given above. A delegate type is a type derived from the System.Delegate system type. Delegate types are implicitly sealed, so it is not permissible to derive any type from a delegate type. It is also not permissible to derive a non-delegate class type from System.Delegate.
The language provides a special syntax for delegate creation and invocation. Except for instantiation, any operation that can be applied to a class or class object instance can also be applied to a delegate class or delegate instance, respectively. In particular, it is possible to access members of System.Delegate via the usual member access syntax.
The set of methods encapsulated by a delegate instance is named an invocation list. When a delegate instance is created from a single method, it encapsulates that method, and its invocation list contains only one element. When two non-empty delegates are combined, their invocation lists are concatenated from left to right.
Additional information is contained in the following subsections:
See also: