当前位置:网站首页>Analysis of C # delegation and anonymous method

Analysis of C # delegation and anonymous method

2022-07-26 10:50:00 aLLLiyyy

stay c# Inside, we can use delegates to call methods with the same method signature , Think carefully , Which is the same thing as c The concept of function pointer of language . because c# Inherited too much c The function of , Like a structure , Overload operator , Precompiling instructions , wait . It is c A variation of language .
Remember , Think of delegates as containers that hold functions of the same nature , Then use the container , Visit the function you just installed , It's that simple . First use the keyword delegate To define a delegate container , as follows

public delegate string BuyDelegate(int money);

It is equivalent to defining a common method , However, there is no method body , Why is there no method body ? Because it delegates other methods to execute logic , Therefore, there is no need for the method body to implement its own specific logic .
Then we implement a static method , The delegate defined above should have function parameters to enjoy , Number of function parameters , as well as returnType( Return type ).

 public static string BuyCorei3(int money) {
            return "core-i3 money:"+money;
        }

Then load this function into the delegate , Then use the delegate instance to trigger the call as follows

BuyDelegate buy = new BuyDelegate(BuyCorei3);// load ,
buy(1100);// Trigger function with delegate 

Okay , It's that simple , Here's the full code , Incidental Anonymous methods implement delegate instances .

using System;

namespace ConsoleApp1
{
    public delegate string BuyDelegate(int money);
    class Program
    {
        static void Main(string[] args)
        {
            BuyCPU(500);// It encapsulates the delegate call 
            BuyDelegate buy = delegate (int money)// This one uses delegate  keyword   The defined method is equivalent to anonymous method , You can build delegate objects with .
            {
                return "xiaomi smartphone money:" + money;
            };
            Console.WriteLine(buy(1250));
            Console.ReadKey();
        }
        
        public static string BuyDog(int money) {
            return "a small dog money:" + money;
        }

        public static string BuyCorei3(int money) {
            return "core-i3 money:"+money;
        }
        public static string BuyCorei5(int money) {
            return "core-i5 money:"+money;
        }
        public static string BuyCorei7(int money) {
            return "core-i7 money:"+money;
        }
        public static void BuyCPU(int money) {
            BuyDelegate buy=null;
            if (money < 1000)
            {
               buy = new BuyDelegate(BuyCorei3); 
            }
            else if (money < 2000)
            {
                buy = new BuyDelegate(BuyCorei5);
            }
            else {
                buy = new BuyDelegate(BuyCorei7);
            }
            Console.WriteLine(buy(money));
        }

        public static void CalculateTime(BuyDelegate buy,int money) {// This method can intercept all functions with the same properties before and after , Equivalent to decorative design pattern .
            long currentTicks = DateTime.Now.Ticks;
            buy(money);
            long spendTimes = DateTime.Now.Ticks - currentTicks;
            Console.WriteLine("time spend:"+spendTimes);
        }
       
    }
}

原网站

版权声明
本文为[aLLLiyyy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207261042568406.html