C # Delegation

Source: Internet
Author: User
Tags bind instance method mul
C # language Series lectures
Delegate


--------------------------------------------------------------------------------


Delegate

Delegation is a new type of data introduced in C # that is very similar to a function pointer in C + +, and is often used for dynamic method calls that are not bound at compile time. Unlike function pointers, delegates fully implement object-oriented in C #, either by referencing static methods or by referencing instance methods, while function pointers can refer only to static methods. Delegates in C # are also type-safe.

As an object-oriented data type, the use of delegation is divided into three steps: delegate declaration, delegate instantiation, and delegate invocation. A delegation declaration is a data type that defines a method body (a static method or an instance method) that encapsulates a particular parameter type and a return value type, as shown in the following example:

delegate int Compute (int left, int. right);

As you can see, the delegate type compute contains two elements of the method: the parameter type and the return value type. The delegate types and methods can be said to be compatible only if they meet the following two conditions:

1. The number of parameters is the same, and their types are the same in order;

2. The return value is the same.

The same type of delegation is that they declare a type of the same type (the same name). Delegate instance equality means that the method they bind to is the same method, or that the same method consists of a chain of methods in the same order, and that their own type is compatible (as well as the above two conditions) and does not have to be forced with the same name.

Delegating instantiation is the process of binding a delegate type to a particular method, similar to the instantiation of other objects, requiring the new statement, except that the method name that is compatible with the delegate type must be accepted as a parameter to the new statement. If it is an instance method, both the instance object and the method must be provided in the middle of the two dots.

Delegation instantiation can be invoked as if the method was invoked. The following is a complete example that typically shows three steps that typically use delegation:

Using System;

delegate int Compute (int left, int. right);

Delegate type declaration

Class A

{

public int Mul (int num1, int num2)

{return num1*num2;

}

}

Class Test

{

public static int Add (int num1, int num2)

{return num1+num2;

}

static void Main ()

{Compute c1=new Compute (ADD);

Instantiation of static methods

A a=new a ();

Compute c2=new Compute (A.mul);

Instantiation of an instance method

int r1=c1 (2,3);//Delegation Call

int r2=c2 (2,3);//Delegation Call

Console.WriteLine (R1);

Console.WriteLine (R2);

}

}

Delegation combination

A delegation combination is a delegate type that can bind multiple callable methods at the same time. Because of the binding of multiple methods, C # requires that the return type of the combined delegate must be void. Of course, the parameter types of these methods must also be compatible with the parameter types of the combined delegate. The delegation combination uses "+" or "+ +" to merge two delegation types into a new combination delegate type; Use "-" or "-=" To remove a delegate instance that has already been bound to a method or a combination delegate instance that binds multiple methods from a grouped delegation type.

It is important to note that the delegate type of the participating operation must be the same in the composition and removal of the delegation-note that "Same" is not "equal". Look at the following example:

Using System;

delegate void MyDelegate (string s);

Class Test

{

public static void Hello (string s)

{

Console.WriteLine ("Hello, {0}!", s);

}

public static void Goodbye (string s)

{

Console.WriteLine ("Goodbye, {0}!", s);

}

public static void Main ()

{

MyDelegate A, B, C, D;

A = new MyDelegate (Hello);

b =new MyDelegate (Goodbye);

Combination of C = a + b;//delegation

d = Removal of c-a;//delegation

A ("a");

B ("B");

C ("C");

D ("D");

}

}

Program output:

Hello, A!

Goodbye, B!

Hello, C!

Goodbye, C!

Goodbye, D!

You can see that when you delegate a group, the combined delegate type C also binds two methods hello (string s) and Goodbye (string s), which you can see from its output.

Note that the method calls in the combinatorial delegation are sequential, if you modify "c = a + B" in the example above; "for" C=b + A; ", you will see a change in the output sequence.

The removal of the same delegate is also sequential, and it always searches for the delegated instance that needs to be removed from the end of the binding delegate instance-note that the removal is in the delegated instance, not the method. You can modify the example above to see the result:

public static void Main ()

{

MyDelegate A, B, C, D;

A = new MyDelegate (Hello);

b =new MyDelegate (Goodbye);

c = Combination of a+b+a;//delegation

Combination of b+=b;//Delegation

d = Removal of c-b;//delegation

A ("a");

B ("B");

C ("C");

D ("D");

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.