共用方式為


delegate

Defines a reference type that can encapsulate one or more methods with a specific function prototype. Delegates provide the underlying mechanism (acting as a kind of pointer to member function) for events in the common language runtime component model.

access delegate function_declaration

Parameters

  • access (optional)
    The accessibility of the delegate outside of the assembly can be public or private. The default is private. Inside a class, a delegate can have any accessibility.

  • function_declaration
    The signature of the function that can be bound to the delegate. The return type of a delegate can be any managed type. For interoperability reasons, it is recommended that the return type of a delegate be a CLS type.

    To define an unbound delegate, the first parameter in function_declaration should be the type of the this pointer for the object (for more information, see Unbound Delegates).

Remarks

Delegates are multicast: the "function pointer" can be bound to one or more methods within a managed class. The delegate keyword defines a multicast delegate type with a specific method signature.

A delegate can also be bound to a method of a value class, such as a static method.

A delegate has the following characteristics:

  • It inherits from System::MulticastDelegate.

  • It has a constructor that takes two arguments: a pointer to a managed class or NULL (in the case of binding to a static method) and a fully qualified method of the specified type.

  • It has a method called Invoke, whose signature matches the declared signature of the delegate.

When a delegate is invoked, its function(s) are called in the order they were attached.

The return value of a delegate is the return value from its last attached member function.

Delegates cannot be overloaded.

delegate is a context-sensitive keyword; see Context-Sensitive Keywords for more information.

Delegates can be bound or unbound.

When you instantiate a bound delegate, the first argument shall be an object reference. The second argument of a delegate instantiation shall either be the address of a method of a managed class object, or a pointer to a method of a value type. The second argument of a delegate instantiation must name the method with the full class scope syntax and apply the address-of operator.

When you instantiate an unbound delegate, the first argument shall either be the address of a method of a managed class object, or a pointer to a method of a value type. The argument must name the method with the full class scope syntax and apply the address-of operator.

When creating a delegate to a static or global function, only one parameter is required: the function (optionally, the address of the function).

You can detect at compile time if a type is a delegate with __is_delegate (type). For more information, see Compiler Support for Type Traits.

For more information on delegates, see

Example

The following example shows how to declare, initialize, and invoke delegates.

// mcppv2_delegate.cpp
// compile with: /clr
using namespace System;

// declare a delegate
public delegate void MyDel(int i);

ref class A {
public:
   void func1(int i) {
      Console::WriteLine("in func1 {0}", i);
   }

   void func2(int i) {
      Console::WriteLine("in func2 {0}", i);
   }

   static void func3(int i) {
      Console::WriteLine("in static func3 {0}", i);
   }
};

int main () {
   A ^ a = gcnew A;

   // declare a delegate instance
   MyDel^ DelInst;

   // test if delegate is initialized
   if (DelInst)
      DelInst(7);
   
   // assigning to delegate
   DelInst = gcnew MyDel(a, &A::func1);

   // invoke delegate
   if (DelInst)
      DelInst(8);

   // add a function
   DelInst += gcnew MyDel(a, &A::func2);

   DelInst(9);

   // remove a function
   DelInst -= gcnew MyDel(a, &A::func1);

   // invoke delegate with Invoke
   DelInst->Invoke(10);

   // make delegate to static function
   MyDel ^ StaticDelInst = gcnew MyDel(&A::func3);
   StaticDelInst(11);
}
in func1 8
in func1 9
in func2 9
in func2 10
in static func3 11

Requirements

Compiler option: /clr

See Also

Concepts

Language Features for Targeting the CLR