How to use lambda to implement delegate event hooks in C #

Source: Internet
Author: User
Tags socket
In writing a small program, encountered such a problem, need to use a delegate to hook up the event, but want to use the local variables in this event, and once the delegate has been defined, the hook will not be able to add additional formal parameters. Is there any way to achieve it

The delegate is defined as follows:

Copy Code code as follows:


public class SOCKETSP


{


public delegate void ReceiveCompleted (byte[] receivebuffer, int receivetotallen,exception ex);


public receivecompleted receivecompleted;


}


The hook side is defined as follows

Copy Code code as follows:


public class Linkouter


{


socketsp linkoutersocket = new socketsp ();


private void Test (Socket requesthandlesocket)
{
To hook up the linkoutersocket.receivecompleted event here, you want to pass the parameter Requesthandlesocket in for subsequent processing.
}
}


The first idea was to use delegate, but failed. Because although the hook up, the delegate passed the parameters are lost, can not be followed up.

Copy Code code as follows:


private void Test (Socket requesthandlesocket)


{


linkoutersocket.receivecompleted + = delegate {


//to do


};


}


The second idea was to use the action and the result failed. The IDE hints that the delegate action does not take 3 parameters.

Copy Code code as follows:


private void Test (Socket requesthandlesocket)


{


linkoutersocket.receivecompleted + = (Action) (Outerreceivebuffer, Totallen, ex) => {


//to do


});


}


The third idea is to use the lambda expression to hook up with the delegate, and to use the call of local variables to implement the parameters passed into the Sendresponse function for subsequent operations.

Copy Code code as follows:


private void Test (Socket requesthandlesocket)


{


linkoutersocket.receivecompleted + + new socketsp.receivecompleted ((Outerreceivebuffer,totallen,ex) =>


{


byte[] Realouterreceivebuffer = new Byte[totallen];


array.copy (outerreceivebuffer, 0, Realouterreceivebuffer, 0, Totallen);


sendresponse (Requesthandlesocket, Realouterreceivebuffer, "OK", "text/html");


});


}


Finally, the lambda expression is implemented.

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.