1 out of 2 rated this helpful - Rate this topic

Delegate Class

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.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[<AbstractClass>]
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
[<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)>]
type Delegate =  
    class 
        interface ICloneable 
        interface ISerializable 
    end

The Delegate type exposes the following members.

  NameDescription
Protected methodDelegate(Object, String)Initializes a delegate that invokes the specified instance method on the specified class instance.
Protected methodDelegate(Type, String)Initializes a delegate that invokes the specified static method from the specified class.
Top
  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryMethodGets the method represented by the delegate.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsTargetGets the class instance on which the current delegate invokes the instance method.
Top
  NameDescription
Public methodSupported by the XNA FrameworkCloneCreates a shallow copy of the delegate.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsCombine(Delegate[])Concatenates the invocation lists of an array of delegates.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCombine(Delegate, Delegate)Concatenates the invocation lists of two delegates.
Protected methodSupported by the XNA FrameworkCombineImplConcatenates the invocation lists of the specified multicast (combinable) delegate and the current multicast (combinable) delegate.
Public methodStatic memberSupported by Portable Class LibraryCreateDelegate(Type, MethodInfo)Creates a delegate of the specified type to represent the specified static method.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCreateDelegate(Type, Object, MethodInfo)Creates a delegate of the specified type that represents the specified static or instance method, with the specified first argument.
Public methodStatic memberCreateDelegate(Type, Object, String)Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance.
Public methodStatic memberCreateDelegate(Type, MethodInfo, Boolean)Creates a delegate of the specified type to represent the specified static method, with the specified behavior on failure to bind.
Public methodStatic memberCreateDelegate(Type, Type, String)Creates a delegate of the specified type that represents the specified static method of the specified class.
Public methodStatic memberCreateDelegate(Type, Object, MethodInfo, Boolean)Creates a delegate of the specified type that represents the specified static or instance method, with the specified first argument and the specified behavior on failure to bind.
Public methodStatic memberCreateDelegate(Type, Object, String, Boolean)Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance with the specified case-sensitivity.
Public methodStatic memberCreateDelegate(Type, Type, String, Boolean)Creates a delegate of the specified type that represents the specified static method of the specified class, with the specified case-sensitivity.
Public methodStatic memberCreateDelegate(Type, Object, String, Boolean, Boolean)Creates a delegate of the specified type that represents the specified instance method to invoke on the specified class instance, with the specified case-sensitivity and the specified behavior on failure to bind.
Public methodStatic memberCreateDelegate(Type, Type, String, Boolean, Boolean)Creates a delegate of the specified type that represents the specified static method of the specified class, with the specified case-sensitivity and the specified behavior on failure to bind.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsDynamicInvokeDynamically invokes (late-bound) the method represented by the current delegate.
Protected methodDynamicInvokeImplDynamically invokes (late-bound) the method represented by the current delegate.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualsDetermines 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 methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)

In XNA Framework, this member is overridden by Finalize().
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeReturns a hash code for the delegate. (Overrides Object.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetInvocationListReturns the invocation list of the delegate.
Protected methodGetMethodImplGets the static method represented by the current delegate.
Public methodGetObjectDataNot supported.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsRemoveRemoves the last occurrence of the invocation list of a delegate from the invocation list of another delegate.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsRemoveAllRemoves all occurrences of the invocation list of a delegate from the invocation list of another delegate.
Protected methodSupported by the XNA FrameworkRemoveImplRemoves the invocation list of a delegate from the invocation list of another delegate.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualityDetermines whether the specified delegates are equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsInequalityDetermines whether the specified delegates are not equal.
Top
  NameDescription
Public Extension MethodSupported in .NET for Windows Store appsGetMethodInfoGets an object that represents the method represented by the specified delegate. (Defined by RuntimeReflectionExtensions.)
Top

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.

NoteNote

The common language runtime provides an Invoke method for each delegate type, with the same signature as the delegate. You do not have to call this method explicitly from C#, Visual Basic, or Visual C++, because the compilers call it automatically. The Invoke method is useful in reflection when you want to find the signature of the delegate type.

The common language runtime provides each delegate type with BeginInvoke and EndInvoke methods, to enable asynchronous invocation of the delegate. For more information about these methods, see Calling Synchronous Methods Asynchronously.

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.

For more information on delegate binding, see the CreateDelegate(Type, Object, MethodInfo, Boolean) method overload.

NoteNote

In the .NET Framework versions 1.0 and 1.1, a delegate can represent a method only if the signature of the method exactly matches the signature specified by the delegate type. Thus, only the first and third bullets in the preceding list are supported, and the first bullet requires an exact type match.

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 a null reference (Nothing in Visual Basic). A combining operation returns 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.

NoteNote

Managed languages use the Combine and Remove methods to implement delegate operations. Examples include the AddHandler and RemoveHandler statements in Visual Basic and the += and -= operators on delegate types in C#.

Starting with the .NET Framework 4, generic delegate types can have variant type parameters. Contravariant type parameters can be used as parameter types of the delegate, and a covariant type parameter can be used as the return type. This feature allows generic delegate types that are constructed from the same generic type definition to be assignment-compatible if their type arguments are reference types with an inheritance relationship, as explained in Covariance and Contravariance in Generics.

NoteNote

Generic delegates that are assignment-compatible because of variance are not necessarily combinable. To be combinable, the types must match exactly. For example, suppose that a class named Derived is derived from a class named Base. A delegate of type Action<Base> (Action(Of Base) in Visual Basic) can be assigned to a variable of type Action<Derived>, but the two delegates cannot be combined because the types do not match exactly.

If an invoked method throws an exception, the method stops executing, the exception is passed back to the caller of the delegate, and remaining methods in the invocation list are not invoked. Catching the exception in the caller does not alter this behavior.

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.

The closest equivalent of a delegate in C or C++ is a function pointer. A delegate can represent a static method or an instance method. When the delegate represents an instance method, the delegate stores not only a reference to the method's entry point, but also a reference to the class instance. Unlike function pointers, delegates are object oriented and type safe.

The following example shows how to define a delegate named myMethodDelegate. Instances of this delegate are created for an instance method and a static method of the nested mySampleClass class. The delegate for the instance method requires an instance of mySampleClass. The mySampleClass instance is saved in a variable named mySC.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.