. NET Foundation Supplements (4) Delegate and event 1

Source: Internet
Author: User

Onedelegate Peek: An object that owns a method

(1) Essence: An object that holds one or more methods; a delegate differs from a typical object in that the execution of a delegate actually executes the method it "holds".

(2) How to use a delegate?

① declaring delegate type (delegate keyword)

② declaring a delegate variable using the delegate type

③ Adding a method to a delegate type

④ Invoke Delegate Execution method

(3) The constancy of the entrustment:

The combination delegate, the delegate = = Increment method, and the delegate-to-= Removal method Let us look like the delegates were modified, but they were not modified. In fact, the delegate is constant .

  What actually happens when you increment and remove a method for a delegate is to create a new delegate whose invocation list is the result of the addition and removal of the method.

delegate void Printfunction (); class test{public   void Print1 ()//instance method   {      Console.WriteLine ("Print1-- Instance ");   }   public static void Print2 ()//static method   {      Console.WriteLine ("Print2-Static");}   } Class program{   static void Main ()   {      Test t = new Test ();                       Printfunction PF;                              PF = t.print1;       PF + = Test.print2;      PF + = T.print1;      PF + = Test.print2;      if (PF! = null)                               {          pf ();           }                                else       {             Console.WriteLine ("Delegate is Empty");        }   }}
Ii. Anonymous Methods

In a method held by a delegate, if a method is used only once, in which case it is not necessary to create a separate named method in addition to the need to create the delegate syntax. Anonymous methods came into being.

  An anonymous method is a method that inline (inline) declares when the delegate is initialized.

Using System;class program{  delegate int otherdel (int inparam);  static void Main ()  {    Otherdel del = delegate (int x)                   {                     return x + +;                   };    Console.WriteLine ("{0}", Del (5));    Console.WriteLine ("{0}", Del (6));}  }
Third, lambda expression: Another form of anonymous method, easier to read

(1) Essence: "Grammatical sugar" which simplifies grammar;

(2) Key points:

The list of parameters in the ①LAMBDA expression (number of parameters, type and location) must match the delegate;

A parameter list in a ② expression does not necessarily need to contain a type unless the delegate has a ref or out keyword (the declaration must be displayed at this time);

③ if there are no arguments, a set of empty parentheses must be used;

(3) Syntax:

All lambda expressions use the lambda operator =>, and the operator reads "goes to". The left side of the lambda operator is the input parameter (if any), and the right is the expression or statement block.

Reference article:

Http://www.cnblogs.com/edisonchou/p/3704510.html

Http://www.cnblogs.com/kingmoon/archive/2011/05/03/2035696.html

. NET Foundation Supplements (4) Delegate and event 1

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.