C # 's delegate, event, Lambda

Source: Internet
Author: User

DelegateWhat is a delegate?

Delegates are a special mechanism for encapsulating methods that help programmers to invoke the appropriate methods more flexibly.

Use of Delegates

A delegate is an object that can reference a method, so when you create a delegate, you actually create an object that is referenced by a storage method. During a program run, the same delegate can be used to invoke different methods, as long as the method it references is changed. Therefore, the method of the delegate invocation is not determined at compile time, but is determined at run time.

Declaration of a delegate

<span style= "Font-family:microsoft yahei;font-size:18px;" >public delegate void Delegateclass () </span>

How delegates are used

(1) Delegateclass dc1=new delegateclass (PROGRAM.FANGFA1);

(2) Delegateclass dc2=new delegateclass (FANGFA1);

(3) Delegateclass dc3=fangfa1;

(4) AClass ac=new aclass ();

Delegateclass dc4=new delegateclass (AC. FANGFA2);

Multicast delegation

A multicast delegate is the ability to allow the creation of a method's invocation list or linked lists. These methods are automatically executed when the multicast delegate is called. If the delegate has a return value, the return value of the last method in the list is used as the return value for the entire delegate invocation. Method List creation: First instantiate a delegate, and then use the "+" or "+ =" operator to add a method to the list. When removing a method, you can use the "-" or "-=" operator.

<span style= "Font-family:microsoft yahei;font-size:18px;" >public delegate void StrOp (ref string str) StrOp StrOp; StrOp op1=hanshu1; StrOp op2=hanshu2;strop=op1;//Remove Method 1, Add Method 2strop-=op1;strop+=op2;</span>

Events

Events are built on the basis of a delegate, which is a signaling mechanism that automatically notifies when certain operations occur. An event is defined as a message sent by an object that signals the occurrence of a notification operation.

declaring Events

public event Abcdelegate Abcevent;

Abcdelegate is the delegate name, Abcevent; is the event name

Using Events(1) Subscribe to Events

EventName + = Delegatename

EventName: Event Name

Delegatename: Method Name

(2) Unsubscribe events

EventName-= Delegatename

Lambda

Lambda is used to create anonymous methods

Expression: param=>expr

Param (parametric)

Expr (expression)

<span style= "Font-family:microsoft yahei;font-size:18px;" >public delegate int adelegate (int abm) class program{      static void Main (string[] args)      {             adelegate a= count=>count+5;             int x=0;             for (int i=0;i<10;i++)            {                   x=a (x);                   Console.WriteLine (x);            }            Console.ReadLine ();       }} </span>


Ps:

Both the event and the Lambda anonymous method are based on the delegate and are implemented by the delegate object.

C # 's delegate, event, Lambda

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.