How to use. net delegation and its benefits

usage method:

//Definition method of no return value and no parameter delegate
public delegate void NoReturnPara();

Several ways to assign values to delegates

//Instantiate the delegate and pass in the method
NoReturbNoPara noreturn = new NoReturbNoPara(this.DoNothing);
NoReturbNoPara noreturn = () => { };
//Direct assignment
NoReturbNoPara noreturn = this.DoNothing;

//Method to call delegate


As shown above, the basic definition of delegation is the usage method. In mvc, this kind of writing method is basically abandoned, and the encapsulated generic delegation is used instead

usage method:

//No return value, no parameter delegate, no need to declare separately
Action act = this.DoNothing;
//No return value, parameter delegate, parameter type is generic
Action<string> act = p => { };
//The return type is string,Parameter type is string Commission of
Func<string,string> func = p => p;

//The return type is bool,Parameter type is string Commission of
Func<string,bool> func = p => p.Equals('');


Here are a few simple demo

    /// <summary>
    /// Extension method
    /// </summary>
    public static class DelegateExtend
        /// <summary>
        /// imitate Linq Of Where operation
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="scoure">data source</param>
        /// <param name="func">Delegation (custom bool Conditions)</param>
        /// <returns></returns>
        public static IEnumerable<T> ExtWhere<T>(this IEnumerable<T> scoure, Func<T, bool> func)
            //Data traversing the data source
            foreach (var item in scoure)
                //Filter return of request delegation completion criteria bool
                bool bResult = func(item);
                //Return the data source submitted through filtering
                if (bResult)
                    yield return item;

Let's call this method to see how to use the delegation

   /// <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 data
            IEnumerable<Student> student = sql.QueryList<Student>();
            //Define an anonymous method and assign it to the delegate
            Func<Student, bool> func = delegate(Student s)
                //Custom code logic, returning bool type
                return s.Id.Equals("1");
            //Incoming delegation
            IEnumerable<Student> list = student.ExtWhere(func);

        //The second method, using linq Syntax (custom logic)
            IEnumerable<Student> list1 = student.ExtWhere(p => p.Id.Equals("1"));

The above is a simple but common delegation scenario

At the beginning, I thought it was very difficult to understand the entrustment. I always felt dizzy, but after I had nothing to do and practiced more, I would understand it very well

Here's a summary of the benefits of using delegates

Decouple: extract custom logic, keep the same logic, and separate the code

Maximize code simplification: decouple while reducing the amount of code

Tags: C# SQL

Posted on Mon, 04 May 2020 03:17:53 -0700 by mlampard