C # study delegation

Source: Internet
Author: User

 

 

  • A delegate is essentially a class that is implicitly inherited from system. multicastdelegate class, which maintains a list of delegates with links. When calling multicast delegates, it is called according to the delegation order of the delegate list, this class also includes a constructor that accepts two parameters and three important methods: begininvoke, endinvoke, and invoke;
  • Multicastdelegate has a list of delegates with links. This list is called a call list and contains one or more elements. When you call multicast delegates, these delegates are synchronously called in the order in which the delegates appear in the call list .. NET provides a concise syntax to create a commission chain: bind and unbind with the + = and-= operators;
  •  
  • Everything is in the Code. In fact, it is okay to understand the delegate type and delegate type variables. In fact, the delegate is just an abstract class like struct and enum, you can define many specific types. These types are the delegate types in the true sense.

 

1 # define debug 2 3 4 using system; 5 using system. collections. generic; 6 using system. LINQ; 7 using system. text; 8 using system. io; 9 using system. threading; 10 11 12 namespace test13 {14 class delegateex15 {16 // define a delegate type. Note that calculatedelegate is only a type, which is the same as defining a class or struct. It only defines a type. 17 // re-emphasize that this product is not a delegate, but a type! This product is also called the delegate type. It is a type, not a delegate! 18 // This type has only one aspect: it indicates the signature of its callback Method -- the return type is void, two int type parameters; 19 // when the delegate type variables are instantiated, you need a method as a parameter. This parameter must comply with the method signature; 20 // The delegate can be understood as a function pointer in C ++ (only the function pointer does not have the concept of multicast/delegate chain) 21 public delegate void calculatedelegate (int x, int y); 22 23 24 // declare a variable of the calculatedelegate type. Declare a variable using the type defined in the previous step. It is only a declaration. 25 Private Static calculatedelegate mydelegate; 26 27 28 // define the void add () method. Obviously, this method is 29 public static void add (int x, int y) 30 {31 console that complies with the method signature declared by the delegate type calculatedelegate. writeline (x + y); 32} 33 34 // define void subtract () method 35 public static void subtract (int x, int y) 36 {37 console. writeline (x-y); 38} 39 40 41 static void main (string [] ARGs) 42 {43 // instantiate mydelegate, which can be abbreviated as mydelegate = add; 44 // The add method is actually attached to the delegate chain of mydelegate. It can also be said that the add method is bound to the mydelegate 45 mydelegate = new calculatedelegate (ADD ); 46 47 // bind subtract to/and mount it to mydelegate 48 mydelegate + = subtract; 49 50 // unbind subtract from mydelegate, or remove subtract from mydelegate to 51 mydelegate-= subtract; 52 53 // call the delegate, actually, it is to pass the corresponding parameters into the delegate chain and call the method 54 mydelegate (100,200) on the delegate chain in sequence; 55 56 57 // re-specify the reference of mydelegate, is to clear the delegate chain of mydelegate, and then add the add method to 58 mydelegate = add; 59 60 // Add Method 61 mydelegate + = subtract to mydelegate; 62 63 // call 64 mydelegate (100,200); 65 66 console. readkey (); 67} 68} 69}

 

 

 

 

 

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.