using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp.DDUP { public class DelegateDemo { public void Run() { string orderNo = "Abcde123"; //1.利用方法委托可写成lambda表达式的形式 var t = CheckNo(e => e.StartsWith("A"), orderNo); Console.WriteLine(t); //2.利用扩展方法写法,更优雅 t = orderNo.CheckNo(e => e.Contains("123") && e.Length == 5); Console.WriteLine(t); //3.原始写法也优雅 t = orderNo.StartsWith("A"); Console.WriteLine(t); } /// <summary> /// 这种写法可以给定委托类型的参数,自定义判断规则 /// </summary> /// <param name="func">委托</param> /// <param name="orderNo">字符串</param> /// <returns></returns> public bool CheckNo(Func<string, bool> func, string orderNo) { return func(orderNo); } } /// <summary> /// 扩展方法使写法更优雅 /// </summary> public static class Extension { public static bool CheckNo(this string str, Func<string, bool> func) { return func(str); } } }
标签:System,优雅,CheckNo,func,using,orderNo,用法,public,lambda From: https://www.cnblogs.com/request/p/16847638.html