Share via


Delegate Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class.

Inheritance Hierarchy

System. . :: . .Object
  System..::..Delegate
    System. . :: . .MulticastDelegate

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<SerializableAttribute> _
Public MustInherit Class Delegate
[SerializableAttribute]
public abstract class Delegate
[SerializableAttribute]
public ref class Delegate abstract
[<AbstractClass>]
[<SerializableAttribute>]
type Delegate =  class end
public abstract class Delegate

The Delegate type exposes the following members.

Constructors

  Name Description
Protected method Delegate

Top

Properties

  Name Description
Public property Method Gets the method represented by the delegate.
Public property Target Gets the class instance on which the current delegate invokes the instance method.

Top

Methods

  Name Description
Public methodStatic member Combine Concatenates the invocation lists of two delegates.
Public method Equals Determines whether the specified object and the current delegate are of the same type and share the same targets, methods, and invocation list. (Overrides Object. . :: . .Equals(Object).)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic member Remove Removes the last occurrence of the invocation list of a delegate from the invocation list of another delegate.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Operators

  Name Description
Public operatorStatic member Equality Determines whether the specified delegates are equal.
Public operatorStatic member Inequality Determines whether the specified delegates are not equal.

Top

Remarks

The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types.

Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.

The declaration of a delegate type establishes a contract that specifies the signature of one or more methods. A delegate is an instance of a delegate type that has references to:

  • An instance method of a type and a target object assignable to that type.

  • An instance method of a type, with the hidden this parameter exposed in the formal parameter list. The delegate is said to be an open instance delegate.

  • A static method.

  • A static method and a target object assignable to the first parameter of the method. The delegate is said to be closed over its first argument.

When a delegate represents an instance method closed over its first argument (the most common case), the delegate stores a reference to the method's entry point and a reference to an object, called the target, which is of a type assignable to the type that defined the method. When a delegate represents an open instance method, it stores a reference to the method's entry point. The delegate signature must include the hidden this parameter in its formal parameter list; in this case, the delegate does not have a reference to a target object, and a target object must be supplied when the delegate is invoked.

When a delegate represents a static method, the delegate stores a reference to the method's entry point. When a delegate represents a static method closed over its first argument, the delegate stores a reference to the method's entry point and a reference to a target object assignable to the type of the method's first argument. When the delegate is invoked, the first argument of the static method receives the target object.

The invocation list of a delegate is an ordered set of delegates in which each element of the list invokes exactly one of the methods represented by the delegate. An invocation list can contain duplicate methods. During an invocation, methods are invoked in the order in which they appear in the invocation list. A delegate attempts to invoke every method in its invocation list; duplicates are invoked once for each time they appear in the invocation list. Delegates are immutable; once created, the invocation list of a delegate does not change.

Delegates are referred to as multicast, or combinable, because a delegate can invoke one or more methods and can be used in combining operations.

Combining operations, such as Combine and Remove, do not alter existing delegates. Instead, such an operation returns a new delegate that contains the results of the operation, an unchanged delegate, or null Nothing nullptr unit a null reference (Nothing in Visual Basic) . A combining operation returns null Nothing nullptr unit a null reference (Nothing in Visual Basic) when the result of the operation is a delegate that does not reference at least one method. A combining operation returns an unchanged delegate when the requested operation has no effect.

When the signature of the methods invoked by a delegate includes a return value, the delegate returns the return value of the last element in the invocation list. When the signature includes a parameter that is passed by reference, the final value of the parameter is the result of every method in the invocation list executing sequentially and updating the parameter's value.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System Namespace