C # core Foundation-delegation,

Source: Internet
Author: User

C # core Foundation-delegation,

A delegate is a type. The delegate in C # Is Object-Oriented, And it is type-safe. When you create a delegate instance, the created instance contains a call list, the call list contains multiple methods. Each method is called a calling entity. The Calling object can be a static method or an instance method. If it is an instance method, the calling entity contains the instance that calls the instance method. The delegate does not care about the class to which the called method belongs. It only cares about whether the called method is compatible with the delegate type. The following is a code example:

1 using System; 2 namespace LycheeTest {3 public delegate void D (int a, int B); 4 public class Test {5 public D myDelegate; 6 public Test () {7 myDelegate = new D (Show1); 8} 9 private static void Show1 (int a, int B) {10 Console. writeLine ("The Show1 method is called. The values of the two arguments are: {0}", a + B); 11} 12 private void Show2 (int a, int B) {13 Console. writeLine ("The Show2 method is called. The values of the two arguments are: {0}", a + B); 14} 15 private void Show3 (int a, int B) {16 Console. writeLine ("The Show3 method is called, and the values of the two arguments are: {0}", a + B ); 17} 18} 19 public class Program {20 static void Main (string [] args) {21 Test myT = new Test (); 22 myT. myDelegate (33, 22); 23 Console. readKey (); 24} 25} 26 27}

This Code demonstrates the simplest form of delegation.The delegate type can be defined outside the class or inside the class.. This code is defined outside the class. The 3rd line of code defines a delegate type. The key word of the delegate type is delegate. Before the key word, it is the access permission modifier of the delegate type. The keyword is the return type of the delegate type. The return type of the method that is compatible with the delegate type must be the same. The name of the delegate type after the return type. Next is the form parameter list, which specifies the parameter type and number of methods compatible with the delegate type must be the same. The Code in line 5th defines a variable of the delegate type. It is an instance field and the access permission is public. Note that the access permission of the delegate type field must be lower than that of the delegate type or be the same as that of the delegate type. Three methods are defined: Line 1, line 2, and line 4. The first line of code is a static method. This Code demonstrates the simplest delegate method, so only the static method is used. In the constructor of Row 3, a variable of the delegate type is instantiated. Note that to add a method to the call list of the delegate variable, you only need to pass the method name to the constructor. This is the most basic method for adding a call method to a delegate. Row 21st defines an instance of the Test class, and then row 22nd calls the delegate member of the class. When you call a delegate member, you must pass real parameters to the parameter list. This is the most basic way to use delegation. The execution result of this Code is as follows:

 

The Show1 method is called. The value of the two arguments is 55.

The following describes how to use a delegate type. The instance code is as follows:

1 using System; 2 namespace LycheeTest {3 public delegate void D (int a, int B); 4 public class Test {5 public static void Show1 (int a, int B) {6 Console. writeLine ("The Show1 method is called. The values of the two arguments are: {0}", a + B); 7} 8 public void Show2 (int a, int B) {9 Console. writeLine ("The Show2 method is called. The values of the two arguments are: {0}", a + B); 10} 11 public void Show3 (int a, int B) {12 Console. writeLine ("The Show3 method is called, and the values of the two arguments are: {0}", a + B ); 13} 14} 15 public class Program {16 static void Main (string [] args) {17 Test myT = new Test (); 18 D myDelegate = new D (Test. show1); 19 D myDelegate1 = new D (myT. show2); 20 D myDelegate2 = new D (myT. show3); 21 myDelegate (22, 33); 22 myDelegate1 (33, 44); 23 myDelegate2 (55, 66); 24 Console. readKey (); 25} 26} 27 28}

This Code removes the delegate type field from the class, but treats the delegate type as a class. In the class that contains the entry point method, first, line 1 defines a variable of the Test class and instantiate it. To pass the instance method of the class to the delegate, you must have an instance of the class to reference the instance method of the class. Row 3 defines a delegate type variable and instantiate it,Note that the delegate is not a member of the class, so when passing a static method to its constructor, you must reference it with the class name.. Row 19th also defines a delegate type variable. When passing an instance method to it, it must be referenced by an instance of the class. The 20th lines of code are the same as the 19th lines of code. When passing a method to a delegate, you need to pass the method name without the list of parameters of the method. Rows 21st to 23rd are called by the delegate. In this case, you must pass the real parameters of the method for the delegate. The execution result of this Code is as follows:

Method Show1 is called. The value of adding two arguments is: 55. Method Show2 is called. The value of adding two arguments is: 77. Method Show3 is called. The value of adding two arguments is: 121.
Delegated access modifier

When the delegate is located outside the class, the access modifiers that can be used include public and internal. If nothing is written, the default value is internal. When the delegate is inside the class, the access modifiers that can be used include public, protected, internal, and protected.

1 using System; 2 namespace LycheeTest {3 public class Test {4 protected delegate void D (int a, int B); 5 private delegate void D1 (int a, int B ); 6 protected internal delegate void D2 (int a, int B); 7 internal delegate void D3 (int a, int B); 8 private D myD; 9 private D1 myD1; 10 private D2 myD2; 11 private D3 myD3; 12 public Test () {13 myD = new D (Show1); 14 myD1 = new D1 (Show1 ); 15 myD2 = new D2 (Show1); 16 myD3 = new D3 (Show1); 17} 18 public static void Show1 (int a, int B) {19 Console. writeLine ("The Show1 method is called. The values of the two arguments are: {0}", a + B); 20} 21 public void Show2 (int a, int B) {22 Console. writeLine ("The Show2 method is called. The values of the two arguments are: {0}", a + B); 23} 24 public void Show3 (int a, int B) {25 Console. writeLine ("The Show3 method is called. The values of the two arguments are: {0}", a + B); 26} 27 public void Use () {28 myD (11, 12); 29 myD1 (22, 45); 30 myD2 (55,78); 31 myD3 (345,100); 32} 33} 34 class Test1: Test {35 private D Test1D; 36 private D2 Test1D2; 37 private D3 Test1D3; 38 public Test1 () {39 Test1D = new D (Test. show1); 40 Test1D2 = new D2 (Test. show1); 41 Test1D3 = new D3 (Test. show1); 42} 43 public void Use1 () {44 Test1D (22, 45); 45 Test1D2 (44, 45); 46 Test1D3 (77, 78 ); 47} 48} 49 public class Program {50 static void Main (string [] args) {51 Test1 myT1 = new Test1 (); 52 myT1.Use (); 53 myT1.Use1 (); 54 Console. readKey (); 55} 56} 57}

Line 2 of the Code defines the delegate type inside the class. It is defined as a member of the class. The access permission is protected, which can be accessed inside the class or by the derived class. The authorization type defined in line 5th of the Code. The access permission is private and can only be accessed by the class. The delegate type of the protected internal access permission defined in line 2 of the code can be accessed by the current Assembly or by the derived class, regardless of the assembly in which the derived class is located. The delegate type defined in line 7th is internal, which can only be accessed by this Assembly. All these types of delegation can be accessed within the class. Therefore, rows 10th to 13th define their variables. In the instance constructor of row 12th, the variables of these four Delegate types are instantiated and the Show1 method is added to their call list.Show1 is a static method, but it does not need to be referenced by the class name when the constructor type is passed in within the class.Row 27th defines the instance method, calls the four delegates within the method, and passes in real parameters for the delegate. The Code in line 34th defines another class, which inherits from the base class Test. Because only the delegate types d, D2, and D3 in the base class can be accessed by the derived classes, the variables of rows 35th to 37th are defined. Note that although they are of the same type as the delegate variables in the base class, they are different delegates. In the instance constructor of Row 3, create instances for the three Delegate types and add methods to the call list,Because the static method Show1 is also inherited by the derived class, the passed method name can be referenced by the class name or not referenced by the class name.. Row 43rd defines an instance method. The method calls these three delegates internally and passes in real parameters for them. Row 51st defines the instance of the derived class and then calls the instance methods Use and Use1. The execution result of this Code is as follows:

The Show1 method is called. The value of the two arguments is: 23. The Show1 method is called. The value of the two arguments is: 67. The Show1 method is called. The value of the two arguments is: the Show1 method of 133 is called. The value of the two arguments is: Show1 method of 445 is called. The value of the two arguments is: Show1 method of 67. The value of the two arguments is: the Show1 method is called. The value of the two arguments is 155.

Because the access permissions of D and D2 are defined as protected and protected internal. So next we will verify whether they can be accessed in other programming sets. First, remove the class containing the Main method in the Code section, and change it to a class library in its project properties. Next, create a console project and reference the class library physically. The console project code is as follows:

 1 using System; 2 using LycheeTest; 3 namespace LycheeTest1{ 4     class Program: Test { 5         private D pD; 6         private D2 pD2; 7         public Program() { 8             pD = new D(Show1); 9             pD2 = new D2(Show1);10         }11         public void Use3() {12             pD(34, 33);13             pD2(12, 11);14         }15         static void Main(string[] args) {16             Program p = new Program();17             p.Use3();18             Console.ReadKey();19         }20     }21 }

Because the namespace of the 3rd-line code and the namespace of the class library are two independent namespaces, their members are not in the same namespace. Therefore, when referencing a member of another namespace in a namespace, you must add the name of another namespace for reference. For ease of coding, the 2nd line of code first references the namespace of the class library. The Code in line 4th defines a class that inherits from the base class Test. Because it is a derived class, you can access the delegate types d and D2. Lines 3 and 3 define the two variables of D and D2 respectively. The instance constructor of row 7th instantiated the two variables and passed the Show1 Method for them. Because the Show1 method is inherited, Class Name Reference is not required here. The 11th line of code defines an instance method. Its function is to call the two delegates and pass in real parameters for them. The Code in line 16th defines an instance of this class and calls the instance method use3. The execution result of this Code is as follows:

The Show1 method is called. The value of the two arguments is: 67. The Show1 method is called. The value of the two arguments is 23.
Both the delegate types D2 and D3 in class Test have the internal permission. Now let's verify whether non-Derived classes in the same assembly can access them. First, change the class library back to the console project, and then add a class, which is independent of the Test class. They are only in the same assembly and have no inheritance relationship with each other. The Code is as follows:
1 using System; 2 namespace LycheeTest {3 public class Test {4 protected delegate void D (int a, int B); 5 private delegate void D1 (int a, int B ); 6 protected internal delegate void D2 (int a, int B); 7 internal delegate void D3 (int a, int B); 8 private D myD; 9 private D1 myD1; 10 private D2 myD2; 11 private D3 myD3; 12 public Test () {13 myD = new D (Show1); 14 myD1 = new D1 (Show1 ); 15 myD2 = new D2 (Show1); 16 myD3 = new D3 (Show1); 17} 18 public static void Show1 (int a, int B) {19 Console. writeLine ("The Show1 method is called. The values of the two arguments are: {0}", a + B); 20} 21 public void Show2 (int a, int B) {22 Console. writeLine ("The Show2 method is called. The values of the two arguments are: {0}", a + B); 23} 24 public void Show3 (int a, int B) {25 Console. writeLine ("The Show3 method is called. The values of the two arguments are: {0}", a + B); 26} 27 public void Use () {28 myD (11, 12); 29 myD1 (22, 45); 30 myD2 (55,78); 31 myD3 (345,100); 32} 33} 34 35 class Test1 {36 private Test. d2 tD2; 37 private Test. d3 tD3; 38 public Test1 () {39 tD2 = new Test. d2 (Test. show1); 40 tD3 = new Test. d3 (Test. show1); 41} 42 public void Use3 () {43 tD2 (34, 33); 44 tD3 (22, 21 ); 45} 46} 47 public class Program {48 static void Main (string [] args) {49 Test1 myT1 = new Test1 (); 50 myT1.Use3 (); 51 Console. readKey (); 52} 53} 54}

In this Code, the original class Test is not modified. On row 3, a class is defined, which is independent of the Test class. Their relationships are limited to the same assembly. Line 2 and line 3 define two variables of the delegate type D2 and D3. Note that because these two classes are not inherited, you must use the Class Name of the Test class to reference the two Delegate types in the Test class. The second line of code is the instance constructor method. In the constructor method, delegates are instantiated. When instantiating a delegate type, you still need to use the class name to reference the delegate type name. The same is true for the passed method name. Row 42 defines an instance method, which calls a delegate and passes in real parameters for it. Line 3 defines an instance of the Test1 class, and then line 3 calls the instance method of the class. The execution result of this Code is as follows:

The Show1 method is called. The values of the two arguments are: 67. The Show1 method is called. The values of the two arguments are: 43.

 

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.