步步为营-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]); } } } }
那么通过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]); } } } }
总结
del getUpper = delegate(string str) { return str.ToUpper(); };
del getUpper = (str) { return str.ToUpper(); };