• 步步为营-24-委托


    委托:将函数作为参数传递 

    例如:操作将一个字符串数组转化为一下大写,小写,转变和打印,普通方法这么写

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Delegate
    {
        class Program
        {
           public static void Main(string[] args)
            {
               //定义字符串
                string[] str = {"Abc","Def","GhIJk"};
    
                MakeChange(str);
                Show(str);
                GetUpper(str);
                Show(str);
                GetLower(str);
                Show(str);
                Console.Read();
            }
    
           public static void GetUpper(string[] strs) 
           {
               for (int i = 0; i < strs.Length; i++)
               {
                   strs[i] = strs[i].ToString().ToUpper();
               } 
           }
    
           public static void GetLower(string[] strs) 
           {
               for (int i = 0; i < strs.Length; i++)
               {
                   strs[i] =  strs[i].ToString().ToLower();
               } 
           }
    
           public static void MakeChange(string[] strs) 
           {
               for (int i = 0; i < strs.Length; i++)
               {
                   strs[i] = "//"+strs[i]+"\\";
               }        
           }
    
           public static void Show(string[] strs) 
           {
               foreach (var item in strs)
               {
                   Console.WriteLine(item);
               }
           }
        }
    }
    普通方法

    这是我们可以使用委托进行改变,我们观察到所有的方法参数和返回值的类型都是一样的.可以声明一个委托

    Delegate string del(string [] strs);

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Delegate
    {
      
        //声明委托
        public delegate string del(string str);
        class Program
        {
           public static void Main(string[] args)
            {
               //定义字符串
                string[] strs = {"Abc","Def","GhIJk"}; 
               //定义委托-法一
                del mc= new del(GetUpper);
                MakeChange(strs , mc);
               //定义委托 -法二
                mc = GetLower;
                MakeChange(strs,mc);
    
                mc = GetChange;
                MakeChange(strs, mc);
               Console.Read();
            }
    
           public static void MakeChange(string[] strs,del Method) 
           {
               for (int i = 0; i < strs.Length; i++)
               {
                strs[i] =  Method(strs[i]);
                Console.WriteLine(strs[i]);
               } 
           }
    
           public static string GetUpper(string strs)
           {
               return strs.ToString().ToUpper();
           }
    
           public static string GetLower(string strs)
           {
               return strs.ToString().ToLower();
           }
    
           public static string GetChange(string strs)
           {
               return "//"+strs.ToString()+"\\";
           }
        }
    }
    委托

    可能会觉得委托并没带来多大简洁,可以通过匿名委托

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Delegate
    {
      
        //声明委托
        public delegate string del(string str);
        class Program
        {
           public static void Main(string[] args)
            {
               //定义字符串
                string[] strs = {"Abc","Def","GhIJk"};
                del getUpper = delegate(string str) { return str.ToUpper(); };
                MakeChange(strs,getUpper);
                del getLower = delegate(string str) { return str.ToLower(); };
                MakeChange(strs, getLower);
                del getChange = delegate(string str) { return "//"+str+"\\"; };
                MakeChange(strs, getChange);
               Console.Read();
            }
    
           public static void MakeChange(string[] strs,del Method) 
           {
               for (int i = 0; i < strs.Length; i++)
               {
                strs[i] =  Method(strs[i]);
                Console.WriteLine(strs[i]);
               } 
           }
    
          
        }
    }
    View Code

    那么通过lmadab表达式呢?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Delegate
    {
      
        //声明委托
        public delegate string del(string str);
        class Program
        {
           public static void Main(string[] args)
            {
               //定义字符串
                string[] strs = {"Abc","Def","GhIJk"};
                del getUpper = (str) => { return str.ToUpper(); };
                MakeChange(strs,getUpper);
                del getLower = (str) => { return str.ToLower(); };
                MakeChange(strs, getLower);
                del getChange = (str) => { return "\\"+str+"//"; };
                MakeChange(strs, getChange);
               Console.Read();
            }
    
           public static void MakeChange(string[] strs,del Method) 
           {
               for (int i = 0; i < strs.Length; i++)
               {
                strs[i] =  Method(strs[i]);
                Console.WriteLine(strs[i]);
               } 
           }
    
          
        }
    }
    L

    总结
    del getUpper = delegate(string str) { return str.ToUpper(); };

    del getUpper = (str) { return str.ToUpper(); };

  • 相关阅读:
    Python基本语法_函数属性 & 参数类型 & 偏函数的应用
    8.2.1.10 Nested-Loop Join Algorithms 嵌套循环算法:
    8.2.1.9 LEFT JOIN and RIGHT JOIN Optimization 左关联和又关联
    8.2.1.8 IS NULL Optimization NULL 优化:
    8.2.1.7 Use of Index Extensions 索引扩展适用
    组合索引,索引内过滤
    8.2.1.6 Index Condition Pushdown Optimization 索引条件内推优化
    clustered index和secondary indexes
    101个MySQL调试和优化技巧
    JavaScript 开发的45个经典技巧
  • 原文地址:https://www.cnblogs.com/YK2012/p/6729191.html
Copyright © 2020-2023  润新知