A detailed example of a delegate in. NET 1.x

Source: Internet
Author: User
The previous article has demonstrated the use of delegates to implement a multi-language greeting program, this article summarizes the use of delegates in. NET 1.x.

Since the delegate is a type (class), it is going to go through a process like declaring a class first, then a new object, and the last call.

Delegates in. NET 1.x

The use of delegates in. NET 1.x is like this, with the following code:

 1 namespace Delegatedemo 2 {3//Declaration delegate 4 public delegate void Mydel (string arg1, String arg2); 5 6 class Prog Ram 7 {8 static void Main (string[] args) 9 {//.net 1.x delegates Class1 c1 = n EW Class1 (); 12 13//Create delegate object Mydel MyDel1 = new Mydel (c1.             Instancemethod);//instance method Mydel MyDel2 = new Mydel (class1.staticmethod);//static method 16 17/Call Delegate 18             MyDel1 ("A", "B");//or Mydel1.invoke ("a", "B"), MyDel2 ("A", "B");//or Mydel2.invoke ("a", "B"); 20 21 Console.readkey ();}23}24 public class Class126 {n-public void Instancemethod (ST Ring arg1, String arg2), {Console.WriteLine (string. Format ("Arg1:{0},arg2:{1}", Arg1, arg2));}31 public static void Staticmethod (string arg1, String arg 2) {Console.WriteLine (string.   Format ("Arg1:{0},arg2:{1}", Arg1, arg2)); 35}36  }37} 

From the above code can be summed up:

1, a delegate can accept either an instance method or a static method, as long as the signature and return value type of the method matches the delegate.

2, there are two ways to invoke a delegate, and the 1th is essentially the Invoke method that invokes the delegate.

Assignment delegate

The above is created using the new Delegatetype () method, which can actually be used to create the delegate in a simpler way. The code for creating the delegate above can be simplified to:

1//Create delegate object 2 Mydel MyDel1 = C1. instancemethod;//instance Method 3 Mydel MyDel2 = class1.staticmethod;//static method

Adding and removing Methods for delegates

The above example, the creation of a delegate only initializes a method, in fact, the delegate can add more than one method, add the method through ' + = ', the removal method through '-= ' to achieve, the following code:

1//Create delegate object 2 Mydel Mydel = C1. Instancemethod;3 Mydel + = C1. Instancemethod2;4 Mydel + = class1.staticmethod;5 Mydel + = CLASS1.STATICMETHOD2;

1, a chain delegate (or multiprocessing) is composed of multiple delegates, and the System.MulticastDelegate class is designed for chained delegates.

2,+= essentially calls the Delegate.combine method.

Invoke delegate

Invoking a delegate is as simple as calling a method, just passing in the arguments required by the delegate, which will be used to invoke each of the methods in the delegate's method list, and is called in sequence, with the following code:

1//Call Delegate 2 Mydel ("AAA", "BBB");

Chained delegates can be called sequentially because System.MulticastDelegate internally maintains a pointer to the next delegate.

Output Result:

Attention:

1, if you add more than one method to the delegate, these methods are called repeatedly.

2, if the delegate has a return value, and the delegate's Invocation method list contains multiple methods, only the return value of the last method is returned, and the other return values are ignored.

Finally, use Xmind to summarize:

Related Article

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.