15. Delegates

Delegates enable scenarios that other languages—such as C++, Pascal, and Modula—have addressed with function pointers. Unlike C++ function pointers, however, delegates are fully object oriented. Also unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method.

A delegate declaration defines a class that is derived from the class System.Delegate. A delegate instance encapsulates an invocation list, which is a list of one or more methods, each of which is referred to as a callable entity. For instance methods, a callable entity consists of an instance and a method on that instance. For static methods, a callable entity consists of just a method. Invoking a delegate instance with an appropriate set of arguments causes each of the delegate’s callable entities to be invoked with the given set of arguments.

An interesting and useful property of a delegate instance is that it does not know or care about the classes of the methods it encapsulates; all that matters is that those methods be compatible (§15.1) with the delegate’s type. This behavior makes delegates perfectly suited for “anonymous” invocation.

15.1 Delegate Declarations

A delegate-declaration is a type-declaration9.6) that declares a new delegate type.

    delegate-declaration:
            attributesopt delegate-modifiersopt delegate return-type identifier
                  type-parameter-listopt (  formal-parameter-listopt )
                         type-parameter-constraints-clausesopt ;

    delegate-modifiers:
            delegate-modifier
            delegate-modifiers  delegate-modifier

    delegate-modifier:
            new
            public
            protected
            internal
            private

It is a compile-time error for the same modifier to appear multiple times in a delegate declaration.

The new modifier is permitted only on delegates declared within another type, in which case it specifies that such a delegate hides an inherited member by the same name, as described in §10.3.4.

The public, protected, internal, and private modifiers control the accessibility of the delegate type. Depending on the context in which the delegate declaration occurs, some of these modifiers may not be permitted (§3.5.1).

The delegate’s type name is identifier.

The optional formal-parameter-list specifies the parameters of the delegate, and the return-type indicates the return type of the delegate.

The optional type-parameter-list specifies the type parameters to the delegate itself.

Delegate types in C# are name equivalent, not structurally equivalent. Specifically, two different delegate types that have the same parameter lists and return type are considered different delegate types. Nevertheless, instances of two distinct but structurally equivalent delegate types may compare as equal (§7.9.8).

For example,

        delegate int D1(int i, double d);
        class A
        {
                public static int M1(int a, double b) {…}
        }
        class B
        {
                delegate int D2(int c, double d);
                public static int M1(int f, double g) {…}
                public static void M2(int k, double l) {…}
                public static int M3(int g) {…}
                public static void M4(int g) {…}
        }

The delegate types D1 and D2 are both compatible with the methods A.M1 and B.M1, as they have the same return type and parameter list. Because these delegate types are different types, however, they are not interchangeable. The delegate types D1 and D2 are incompatible with the methods B.M2, B.M3, and B.M4, because they have different return types or parameter lists.

Like other generic type declarations, type arguments must be given to create a constructed delegate type. The parameter types and return type of a constructed delegate type are created by substituting, for each type parameter in the delegate declaration, the corresponding type argument of the constructed delegate type. The resulting return type and parameter types are used in determining what methods are compatible with a constructed delegate type. For example,

        delegate bool Predicate<T>(T value);
        class X
        {
                static bool F(int i) {…}
                static bool G(string s) {…}
        }

Here, the delegate type Predicate<int> is compatible with the method X.F and the delegate type Predicate<string> is compatible with the method X.G.

The only way to declare a delegate type is via a delegate-declaration. A delegate type is a class type that is derived from System.Delegate. 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 nondelegate class type from System.Delegate. Note that System.Delegate is not itself a delegate type; it is a class type from which all delegate types are derived.

C# provides special syntax for delegate instantiation and invocation. Except for instantiation, any operation that can be applied to a class or class instance can also be applied to a delegate class or instance, respectively. In particular, it is possible to access members of the System.Delegate type via the usual member access syntax.

The set of methods encapsulated by a delegate instance is called an invocation list. When a delegate instance is created (§15.2) from a single method, it encapsulates that method, and its invocation list contains only one entry. However, when two non-null delegate instances are combined, their invocation lists are concatenated—in the order left operand, then right operand—to form a new invocation list, which contains two or more entries.

Delegates are combined using the binary +7.7.4) and += operators (§7.16.2). A delegate can be removed from a combination of delegates, using the binary -7.7.5) and -= operators (§7.16.2). Delegates can also be compared for equality (§7.9.8).

The following example shows the instantiation of a number of delegates, and their corresponding invocation lists:

        delegate void D(int x);
        class C
        {
                public static void M1(int i) {…}
                public static void M2(int i) {…}
        }
        class Test
        {
                static void Main() {
                       D cd1 = new D(C.M1);        // M1
                       D cd2 = new D(C.M2);        // M2
                       D cd3 = cd1 + cd2;               // M1 + M2
                       D cd4 = cd3 + cd1;               // M1 + M2 + M1
                       D cd5 = cd4 + cd3;               // M1 + M2 + M1 + M1 + M2
                }
        }

When cd1 and cd2 are instantiated, each encapsulates one method. When cd3 is instantiated, it has an invocation list of two methods, M1 and M2, in that order. cd4’s invocation list contains M1, M2, and M1, in that order. Finally, cd5’s invocation list contains M1, M2, M1, M1, and M2, in that order. For more examples of combining (as well as removing) delegates, see §15.4.

15.2 Delegate Compatibility

A method or delegate M is compatible with a delegate type D if all of the following are true:

•  D and M have the same number of parameters, and each parameter in D has the same ref or out modifiers as the corresponding parameter in M.

•  For each value parameter (a parameter with no ref or out modifier), an identity conversion (§6.1.1) or implicit reference conversion (§6.1.6) exists from the parameter type in D to the corresponding parameter type in M.

•  For each ref or out parameter, the parameter type in D is the same as the parameter type in M.

•  An identity or implicit reference conversion exists from the return type of M to the return type of D.

15.3 Delegate Instantiation

An instance of a delegate is created by a delegate-creation-expression7.5.10.5) or a conversion to a delegate type. The newly created delegate instance then refers to either

•  The static method referenced in the delegate-creation-expression, or

•  The target object (which cannot be null) and instance method referenced in the delegate-creation-expression, or

•  Another delegate.

For example,

        delegate void D(int x);
        class C
        {
                public static void M1(int i) {…}
                public void M2(int i) {…}
        }
        class Test
        {
                static void Main() {
                       D cd1 = new D(C.M1);                // Static method
                       C t = new C();
                       D cd2 = new D(t.M2);                  // Instance method
                       D cd3 = new D(cd2);                    // Another delegate
                }
        }

Once instantiated, delegate instances always refer to the same target object and method. Remember, when two delegates are combined, or one is removed from another, a new delegate results with its own invocation list; the invocation lists of the delegates combined or removed remain unchanged.

15.4 Delegate Invocation

C# provides special syntax for invoking a delegate. When a non-null delegate instance whose invocation list contains one entry is invoked, it invokes the one method with the same arguments it was given, and returns the same value as the referred-to method. (See §7.5.5.3 for detailed information on delegate invocation.) If an exception occurs during the invocation of such a delegate, and that exception is not caught within the method that was invoked, the search for an exception catch clause continues in the method that called the delegate, as if that method had directly called the method to which that delegate referred.

Invocation of a delegate instance whose invocation list contains multiple entries proceeds by invoking each of the methods in the invocation list, synchronously, in order. Each method so called is passed the same set of arguments as was given to the delegate instance. If such a delegate invocation includes reference parameters (§10.6.1.2), each method invocation will occur with a reference to the same variable; changes to that variable by one method in the invocation list will be visible to methods farther down the invocation list. If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.

If an exception occurs during processing of the invocation of such a delegate, and that exception is not caught within the method that was invoked, the search for an exception catch clause continues in the method that called the delegate, and any methods farther down the invocation list are not invoked.

Attempting to invoke a delegate instance whose value is null results in an exception of type System.NullReferenceException.

The following example shows how to instantiate, combine, remove, and invoke delegates:

        using System;
        delegate void D(int x);
        class C
        {
                public static void M1(int i) {
                       Console.WriteLine("C.M1: " + i);
                }
                public static void M2(int i) {
                       Console.WriteLine("C.M2: " + i);
                }
                public void M3(int i) {
                       Console.WriteLine("C.M3: " + i);
                }
        }
        class Test
        {
                static void Main() {
                       D cd1 = new D(C.M1);
                       cd1(-1);                            // Call M1
                       D cd2 = new D(C.M2);
                       cd2(-2);                            // Call M2
                       D cd3 = cd1 + cd2;
                       cd3(10);                           // Call M1, then M2
                       cd3 += cd1;
                       cd3(20);                           // Call M1, M2, then M1
                       C c = new C();
                       D cd4 = new D(c.M3);
                       cd3 += cd4;
                       cd3(30);                           // Call M1, M2, M1, then M3
                       cd3 -= cd1; // Remove last M1
                       cd3(40);                           // Call M1, M2, then M3
                       cd3 -= cd4;
                       cd3(50);                           // Call M1, then M2
                       cd3 -= cd2;
                       cd3(60);                           // Call M1
                       cd3 -= cd2;                      //  Impossible removal is benign
                       cd3(60);                           // Call M1
                       cd3 -= cd1;                      //  Invocation list is empty so cd3 is null
                       //                cd3(70);        // System.NullReferenceException thrown
                       cd3 -= cd1;                     //  Impossible removal is benign
                }
        }

As shown in the statement cd3 += cd1;, a delegate can be present in an invocation list multiple times. In this case, it is simply invoked once per occurrence. In an invocation list such as the one in this example, when that delegate is removed, the last occurrence in the invocation list is the one actually removed.

Immediately prior to the execution of the final statement, cd3 -= cd1;, the delegate cd3 refers to an empty invocation list. Attempting to remove a delegate from an empty list (or to remove a nonexistent delegate from a nonempty list) is not an error.

The preceding example produces the following output:

        C.M1: -1
        C.M2: -2
        C.M1: 10
        C.M2: 10
        C.M1: 20
        C.M2: 20
        C.M1: 20
        C.M1: 30
        C.M2: 30
        C.M1: 30
        C.M3: 30
        C.M1: 40
        C.M2: 40
        C.M3: 40
        C.M1: 50
        C.M2: 50
        C.M1: 60
        C.M1: 60

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset