The use of. net delegation and the benefits of using delegation,. net Delegation

Source: Internet
Author: User

The use of. net delegation and the benefits of using delegation,. net Delegation

Before using it, let's take a rough look at what is delegation?

Literally, I entrust someone to help me do one thing. For example, for some reason, I cannot say something like Xiaohong. Then I ask Xiao Ming to help me confess to Xiaohong, then I ask James to help me confess this kind of behavior, it is called Commission

A requests B to help me obtain or transmit the behavior, which is called A delegate.

 

Delegate statement method:

// No return value definition method without parameter delegation public delegate void NoReturnPara ();

No parameter, no return value method:

Public void DoNothing () {// No parameter, no return value}

 

Assignment Methods

// Instantiate the delegate and pass in the method NoReturbNoPara noreturn = new NoReturbNoPara (this. doNothing); // LinqToObjectNoReturbNoPara noreturn = () =>{}; // assign NoReturbNoPara noreturn = this directly. doNothing;

// Call the delegate Method

Noreturn. Invoke ()

The above shows the basic definition of delegation in the usage method. In the actual B/S project, this writing method is basically abandoned, and the encapsulated generic delegation is used.

Usage:

// No return value, no parameter delegate, no separate declaration of Action act = this. DoNothing;
// No return value, parameter delegate, parameter type: Generic Action <string> act = p => {};
// The return type is string, and the parameter type is string's delegate Func <string, string> func = p => p; // The return type is bool, delegate Func whose parameter type is string <string, bool> func = p => p. equals ('');

 

Below are some simple demos for demonstration.

/// <Summary> /// Extension Method /// </summary> public static class DelegateExtend {/// <summary> /// mimic the Where operation of Linq /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "scoure"> data source </param> /// <param name = "func"> delegate (custom bool condition) </param> // <returns> </returns> public static IEnumerable <T> ExtWhere <T> (this IEnumerable <T> scoure, Func <T, bool> func) {// print data from the data source foreach (var item in scoure) {// return bool bResult = func (item) for filtering the condition of delegate completion ); // return the data source submitted by filtering out if (bResult) {yield return item ;}}}}

Next we will call this method to see how to use the delegate.

/// <Summary> /// entity model /// </summary> public class Student {// <summary> /// ID /// </summary> public string Id {get; set ;}//< summary> /// Name /// </summary> public string Name {get; set ;}}
// Query all the data IEnumerable <Student> student = SQL. queryList <Student> (); // defines an anonymous method and assigns it to the delegate Func <Student, bool> func = delegate (Student s) {// custom code logic, return bool type return s. id. equals ("1") ;}; // input the delegate IEnumerable <Student> list = student. extWhere (func); // method 2. Use the linq syntax (custom logic) IEnumerable <Student> list1 = student. extWhere (p => p. id. equals ("1 "));

The above is a simple but common scenario for delegated use.

To understand this code,

ExtWhere is one thing I want to do, but in this case, I need a return result of the bool type, so I entrust func to help me get the result of this bool type.

At the beginning, I felt very difficult to understand the delegation. I always felt dizzy. But after I had nothing to do, I would understand it very well.

The demo above explains the benefits of using delegation.

Decoupling: extract the custom logic, retain the same logic, and separate code

Simplified code to the maximum extent: decoupling reduces the amount of code (custom logic can avoid repeated code with the same logic)

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.