C # Delegate Code examples

Source: Internet
Author: User
Tags modifiers
This article will pass the example analysis to the C # Delegate to carry on the detailed introduction, has the very good reference value, below follows the small compilation together to look down

A delegate is a type. A delegate in C # is object-oriented, and it is type-safe when a delegate instance is created, the instance that is created contains a list of calls that can contain multiple methods in the invocation list. Each method is called a calling entity. The calling entity can be a static method, or it can be an instance method. If it is an instance method, the calling entity contains an instance that invokes the instance method. The delegate does not care about the class to which the method is called, it only cares about whether the invoked method is compatible with the type of the delegate. Here is the code example:

Using system;namespace lycheetest{public delegate void D (int a, int. b); public class Test {public D mydelegate; public T EST () {  mydelegate = new D (SHOW1);} private static void Show1 (int a, int b) {  Console.WriteLine ("Method Show1 is called, two real The value of the parameter addition is: {0} ", a + B); } private void Show2 (int a, int b) {  Console.WriteLine ("Method Show2 is called, the value added by two arguments is: {0}", a + b);} private void Show3 (int A, int b) {  Console.WriteLine ("Method Show3 is called, the value added by two arguments is: {0}", a + b);}} public class program {static void Main (string [] args) {  Test MyT = new Test ();  Myt.mydelegate (+);  Console.readkey (); } }}

This code demonstrates the simplest form of a delegate. A delegate type can be defined outside a class, or within a class. This section of code is defined outside the class. The 3rd line of code defines a delegate type, the keyword of the delegate type is delegate, and the keyword is the access modifier of the delegate type. Keyword is the return type of the delegate type, which specifies that the return type of the method compatible with the delegate type must be the same. The return type is followed by the name of the delegate type. Next is the formal parameter list, which specifies that the parameter types and number of methods compatible with the delegate type must be the same. The 5th line of code defines a variable of a delegate type, which is an instance field and the access permission is public. Note The delegate type field must have access permissions that are lower than the delegate type, or that are identical to the delegate type's access rights. Line 9th, line 12th, and 15th lines define three methods. The 9th line of code is a static method. Because this code demonstrates the simplest way to delegate usage, only static methods are used. In the construction method of line 6th, you instantiate a variable of the delegate type, and notice that you add a method to the invocation list of the delegate variable, simply passing the method name to its constructor method. This is the most basic way to add a calling method to a delegate. Line 21st defines an instance of the Test class, and then line 22nd invokes the delegate member of the class. When invoking a delegate member, you need to pass an argument to its formal parameter list. This is the most basic way to use a delegate. The result of this code execution is as follows:

方法 Show1 被调用,两个实参相加的值是:55

The following describes the use of a delegate type, the instance code is as follows:

Using System;namespace lycheetest {public delegate void D (int a, int. b); public class Test {public static void Show1 (int A, int b) {  Console.WriteLine ("Method Show1 is called, the value added by two arguments is: {0}", a + b);} public void Show2 (int a, int b) {  CONSOLE.W Riteline ("Method Show2 is called, the value added by the two arguments is: {0}", a + B);  } public void Show3 (int a, int b) {  Console.WriteLine ("Method Show3 is called, the value added for two arguments is: {0}", a + b);}} public class program { static void Main (string[] args) {  Test MyT = new Test ();  D mydelegate = new D (test.show1);  D myDelegate1 = new D (myt.show2);  D myDelegate2 = new D (MYT.SHOW3);  MyDelegate (a);  MyDelegate1 (,);  MyDelegate2 (+);  Console.readkey (); } }}

This code removes the delegate type field from the class and treats the delegate type as a class. In the class that contains the entry point method, first the 17th line defines a variable for the Test class and instantiates it. Because you want to pass an instance method of a class to a delegate, you must have an instance of the class present to refer to the instance method of the class. The 18th line defines a variable of a delegate type and instantiates it, and it is important to note that because a delegate is not a member of a class, it is necessary to refer to it as a class name when passing a static method to its construction method. Line 19th also defines a variable of the delegate type, which is referred to as an instance of the class when passing an instance method to it. The 20th line of code is the same as the 19th line of code. When passing a method to a delegate, you need to pass the method name without requiring a parameter list of the method. Lines 21st through 23rd are calls to the delegate, at which point the arguments for the method are passed. The result of this code execution is as follows:

Method Show1 is called, the value added by the two arguments is: 55 method Show2 is called, the value added by two arguments is: 77 method Show3 is called, the value added by two arguments is: 121

Delegate's access modifier

When the delegate is outside the class, the access modifiers you can use include public and internal. If you do not write anything, the default is internal. When a delegate is inside a class, the access modifiers you can use include public, protected, internal, protected

Using System;namespace lycheetest{public class Test {protected delegate void D (int a, int b); private delegate void D1 (i NT A, int b); protected internal delegate void D2 (int a, int b); Internal delegate void D3 (int a, int b); Private D MyD; Private D1 myD1; Private D2 myD2; Private D3 myD3;  Public Test () {MyD = new D (SHOW1);  myD1 = new D1 (SHOW1);  myD2 = new D2 (SHOW1); myD3 = new D3 (SHOW1); The public static void Show1 (int a, int b) {Console.WriteLine ("Method Show1 is called, the value added by the two arguments is: {0}", A + B), and the public void is Show2 (in t A, int b) {Console.WriteLine ("Method Show2 is called, the value added by two arguments is: {0}", a + b);} public void Show3 (int a, int b) {Console.writel Ine ("Method Show3 is called, the value added by the two arguments is: {0}", a + B);  } public void Use () {MyD (11, 12);  MyD1 (22, 45);  MyD2 (55, 78); MyD3 (345, 100); }} class Test1:test {private D test1d; private D2 test1d2; private D3 test1d3; public Test1 () {test1d = new D (test.sh  OW1);  TEST1D2 = new D2 (TEST.SHOW1); Test1d3 = new D3 (TEST.SHOW1);  } public void Use1 () {test1d (22, 45);TEST1D2 (44, 45); Test1d3 (77, 78);  }} public class program {static void Main (string[] args) {Test1 myT1 = new Test1 ();  Myt1.use ();  Myt1.use1 (); Console.readkey (); } }}

The 4th line of the code defines the delegate type within the class, which is defined as a member of the class, access is protected, it can be accessed internally by this class, or it can be accessed by a derived class. The 5th line of the code defines the delegate type, and access is private, and it can only be accessed internally by this class. The 6th line of the code defines the delegate type of protected internal access, which can be accessed by this Assembly, and can also be accessed by derived classes, regardless of the assembly in which the derived class resides. The delegate type defined by line 7th is internal and can only be accessed by this Assembly. Because all of these delegate types can be accessed internally by this class, rows 10th through 13th define their variables. In the instance construction method of line 12th, the variables of these four delegate types are instantiated, and method Show1 are added for their invocation lists. Show1 is a static method, but it is not necessary to use a class name reference when passing in a constructor of a delegate type inside a class . Line 27th defines the instance method, invokes the four delegates inside the method, and passes the arguments to them. The 34th line of code also defines a class, which inherits from the base class Test. Because the delegate types in the base class only have D, D2, and D3 that can be accessed by derived classes, rows 35th through 37th define their variables. Note that although they are the same type as the delegate variables in the base class, they are different delegates. In the instance construction method of line 38th, create an instance for the variables of the three delegate types and join the method for its invocation list, because the static method Show1 is also inherited by the derived class, so the method name passed in here can be referenced using the class name, or it can be referenced without using the class name . Line 43rd defines an instance method that invokes the three delegates internally and passes the arguments to them. Line 51st defines an instance of the derived class and then invokes the instance method use and Use1. The result of this code execution is as follows:

Method Show1 is called, the value added by the two arguments is: 23 method Show1 is called, the value added by two arguments is: 67 method Show1 is called, the value added by two arguments is: 133 method Show1 is called, two arguments are added: 445 method Show1 is called, two arguments The added value is: 67 method Show1 is called, the value added by two arguments is: 89 method Show1 is called, the value added by two arguments is: 155

Because the access rights of D and D2 are defined as protected and protected internal. So here's how to verify that they are accessible in other assemblies. First, remove the class that contains the Main method from the code in this section, and then change it to a class library in its project properties. Next, create a new console project and physically reference the class library. The code for the console project is as follows:

Using system;using Lycheetest;namespace lycheetest1{class Program:test {private D PD; private D2 pD2; public program () {  PD = new D (SHOW1);  pD2 = new D2 (SHOW1); } public void Use3 () {  PD ();  PD2 (12, 11); } static void Main (string[] args) {program  p = new program ();  P.use3 ();  Console.readkey (); } }}

Because the namespace of the 3rd line of code and the namespace of the class library are two separate namespaces, their members are not in the same namespace. So when you reference a member of another namespace within one namespace, you need to refer to the name of another namespace. For the convenience of code writing, the 2nd line of code first references the namespace of the class library. The 4th line of code defines a class that inherits from the base class Test. Because it is a derived class, it can be accessed for delegate types D and D2. The 5th line of code and the 6th line of code define the two variables for D and D2, respectively. The instance construction method of line 7th instantiates the two variables and Show1 them into the method. Because the Show1 method is inherited, the class name reference is not required here. The 11th line of code defines an instance method that invokes the two delegates and passes them the arguments. The 16th line of code defines an instance of this class and invokes the instance method Use3. The result of this code execution is as follows:

Method Show1 is called, the value added by the two arguments is: 67 method Show1 is called, the value added by two arguments is: 23

The delegate types D2 and D3 in class test have internal permissions, and now verify that they are accessible to non-derived classes in a single assembly. First change the class library back to the console project and then add a class that is independent of the Test class. They are only in one assembly and do not have an inheritance relationship with each other. The code is as follows:

Using System;namespace lycheetest {public class Test {protected delegate void D (int a, int b); private delegate void D1 ( int a, int b); protected internal delegate void D2 (int a, int b); Internal delegate void D3 (int a, int b); Private D MyD; Private D1 myD1; Private D2 myD2; Private D3 myD3;  Public Test () {MyD = new D (SHOW1);  myD1 = new D1 (SHOW1);  myD2 = new D2 (SHOW1); myD3 = new D3 (SHOW1); The public static void Show1 (int a, int b) {Console.WriteLine ("Method Show1 is called, the value added by the two arguments is: {0}", A + B), and the public void is Show2 (in t A, int b) {Console.WriteLine ("Method Show2 is called, the value added by two arguments is: {0}", a + b);} public void Show3 (int a, int b) {Console.writel Ine ("Method Show3 is called, the value added by the two arguments is: {0}", a + B);  } public void Use () {MyD (11, 12);  MyD1 (22, 45);  MyD2 (55, 78); MyD3 (345, 100);  }} class Test1 {private test.d2 tD2; private test.d3 tD3; public Test1 () {tD2 = new test.d2 (TEST.SHOW1); tD3 = new Test.d3 (TEST.SHOW1);  } public void Use3 () {tD2 (34, 33); TD3 (22, 21); }} public class program {static void Main (string[] args) {Test1 myT1 = new Test1 ();  MYT1.USE3 (); Console.readkey (); } }}

In this code, the original class test was not modified. On line 35th, a class is defined, which is a separate class relative to the test class. Their relationship is limited to the same assembly. The 36th line of code and the 37th line of code define the two variables for the delegate type D2 and D3. It is important to note that because these two classes are not inheritance relationships, it is necessary to refer to the two delegate types in the test class using the class name of the test class. The 38th line of code is an instance construction method that instantiates a delegate in a constructed method. When instantiating a delegate type, you still need to refer to the delegate type name using the class name, as well as the method name passed. Line 42 defines an instance method that invokes the delegate and passes in an argument to it. The 49th line of code defines an instance of the class Test1, and then line 61st invokes an instance method of the class. The result of this code execution is as follows:

Method Show1 is called, the value added by the two arguments is: 67 method Show1 is called, the value added by two arguments is: 43

The above is the C # delegate code examples of detailed content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.