Mono Class Library: System Namespace

System.Delegate Class

A class used to create types that invoke methods. [Edit]

See Also: Delegate Members

[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Delegate : ICloneable, System.Runtime.Serialization.ISerializable

Remarks

Delegate types derive from the Delegate class. The declaration of a delegate type establishes a contract that specifies the signature of one or more methods.

Note: For an example of a delegate type declaration, see the examples at the end of this topic.

Delegate types are implicitly sealed: it is not permissible to derive a new type from a delegate type.

Note: The Delegate class is not considered a delegate type; it is a class used to derive delegate types.

Note: For information on subclassing the Delegate class, see Partition II of the CLI Specification.

A delegate is an instance of a delegate type. A non-null delegate references an invocation list, which is made up of one or more entries. Each entry consists of a pair of values: a non-null method, and a corresponding object, called the target. If the method is static, the corresponding target is null , otherwise the target is the instance on which the method is to be called.

The signature of each method in the invocation list is required to exactly match the signature specified by the delegate's type.

When a delegate is invoked, the methods in the corresponding invocation list are invoked in the order in which they appear in that list. A delegate attempts to invoke every method in its invocation list, with duplicate methods being invoked once for each occurrence in that list.

Delegates are immutable; once created, the invocation list of a delegate does not change. Combining operations, such as Delegate.Combine(Delegate, Delegate) and Delegate.Remove(Delegate, Delegate), cannot alter existing delegates. Instead, such operations result in the return of either a new delegate that contains the results of the operation, an existing delegate, or the null value.

Note: A combining operation returns the null value when the result of the operation is an empty invocation list. A combining operation returns an existing delegate when the requested operation has no effect (for example, if an attempt is made to remove a nonexistent entry).

If an invoked method throws an exception, the method stops executing and the exception is passed back to the caller of the delegate. The delegate does not continue invoking methods from its invocation list. Catching the exception in the caller does not alter this behavior. It is possible that non-standard methods that implement combining operations allow the creation of delegates with different behavior. When this is the case, the non-standard methods are required to specify the 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.

Note: For an example that demonstrates this behavior, see Example 2.

[Edit]

Example

Example1:

The following example creates two delegates. The first delegate invokes a static method, and the second invokes an instance method on a target object.

C# Example
using System;
public delegate string DelegatedMethod(string s);
class MyClass {
 public static string StaticMethod(string s) {
 return ("Static method Arg=" + s);
 }
 public string InstanceMethod(string s) {
 return ("Instance method Arg=" + s);
 }
}
class TestClass {
 public static void Main() {
 MyClass myInstance = new MyClass();
 //Create delegates from delegate type DelegatedMethod.
 DelegatedMethod delStatic = new DelegatedMethod(MyClass.StaticMethod); 
 DelegatedMethod delInstance = new DelegatedMethod(myInstance.InstanceMethod);
 //Invoke the methods referenced by the delegates.
 Console.WriteLine (delStatic("Call 1"));
 Console.WriteLine (delInstance ("Call 2"));
 }
}

The output is

Static method Arg=Call 1
Instance method Arg=Call 2

Example2:

The following example shows the return value and the final value of a parameter that is passed by reference to a delegate that invokes multiple methods.

C# Example
using System;
class MyClass {
 public int Increment(ref int i) {
   Console.WriteLine("Incrementing {0}",i);
   return (i++);
 }
 public int Negate(ref int i) {
   Console.WriteLine("Negating {0}",i);
   i = i * -1;
   return i;
 }
}

public delegate int DelegatedMethod(ref int i);
class TestClass {
 public static void Main() {
   MyClass myInstance = new MyClass();
   DelegatedMethod delIncrementer = new DelegatedMethod(myInstance.Increment);
   DelegatedMethod delNegater = new DelegatedMethod(myInstance.Negate);
   DelegatedMethod d = (DelegatedMethod) Delegate.Combine(delIncrementer, delNegater);
   int i = 1;
   Console.WriteLine("Invoking delegate using ref value {0}",i);
   int retvalue = d(ref i); 
   Console.WriteLine("After Invoking delegate i = {0} return value is {1}",i, retvalue);
  }
}

The output is

Invoking delegate using ref value 1
Incrementing 1
Negating 2
After Invoking delegate i = -2 return value is -2

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0