zoukankan      html  css  js  c++  java
  • c#系统泛型委托

    Action<T> 无返回值的系统泛型委托
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
        class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                List<UserInfo> list = getInit();
                list.ForEach(new Action<UserInfo>(delegate (UserInfo ui) { Console.WriteLine(ui.Name); }));
                Console.WriteLine("----------------------------------------------------------------------------");
                list.ForEach(delegate (UserInfo ui) { Console.WriteLine(ui.Id+"|"+ui.Name); });
                Console.WriteLine("----------------------------------------------------------------------------");
                list.ForEach(u=> {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.ReadLine();
            }
        }
    }
    Predicate<T> 返回bool值的系统泛型委托
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
       static  class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                #region
                List<UserInfo> list = getInit();
                list = list.FindAll(new Predicate<UserInfo>(delegate (UserInfo u) { return u.Id > 1; }));
                list.ForEach(u => {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.WriteLine("----------------------------------------------------------------------------");
                list = list.FindAll(delegate (UserInfo u) { return u.Id > 2; });
                list.ForEach(u => {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.WriteLine("----------------------------------------------------------------------------");
                list=list.FindAll(u => u.Id > 3);
                list.ForEach(u=> {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                #endregion
    
                Console.WriteLine("----------------------------自定义扩展方法---------------------------");
                List<UserInfo> listnew = list.MyFindAll<UserInfo>(delegate (UserInfo u) { return u.Id > 3; });
                listnew.ForEach(u => {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.ReadLine();
            }
    
            static List<T> MyFindAll<T>(this List<T> list, Predicate<T> predicate)
            {
                //新集合
                List<T> newlist = new List<T>();
               //遍历老集合
                foreach (T item in list)
                {
                    //如果item符合条件,则加入新集合
                    if (predicate(item))
                    {
                        newlist.Add(item);
                    }
                }
                return newlist;
            }
        }
    }
    Comparison<T> 返回int类型
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
       static  class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                List<UserInfo> list = getInit();
                list.Sort(delegate (UserInfo u1, UserInfo u2) {
                    return u2.Age - u1.Age;
                });
                list.ForEach(u =>
                {
                    Console.WriteLine(u.Id + "|" + u.Name);
                });
                Console.ReadLine();
            }
        }
    }
    Func<T,TReturn> 自定义返回类型的系统泛型委托
    namespace ConsoleApp1
    {
        public class UserInfo
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }
        public class UserSimple
        {
            public string Name { get; set; }
        }
        static class Program
        {
            private static List<UserInfo> getInit()
            {
                return new List<UserInfo>() {
                    new UserInfo(){ Id=1, Name="001小王",Age=18 },
                    new UserInfo (){ Id=2,Name="002大王",Age=20},
                    new UserInfo (){ Id=3,Name="003笨笨",Age=21},
                    new UserInfo (){ Id=4,Name="004通天塔",Age=24},
                };
            }
            static void Main(string[] args)
            {
                List<UserInfo> list = getInit();
                IEnumerable<UserSimple> uslist = list.Select(new Func<UserInfo, UserSimple>(delegate (UserInfo u) { return new UserSimple() { Name = u.Name }; }));
                uslist.ToList().ForEach(us =>
                {
                    Console.WriteLine( "|" + us.Name);
                });
                Console.WriteLine("----------------------------------------------------------------------------");
                IEnumerable<UserSimple> newuslist = list.Select(delegate (UserInfo u) { return new UserSimple() {  Name = u.Name }; });
                uslist.ToList().ForEach(us =>
                {
                    Console.WriteLine( "|" + us.Name);
                });
                Console.WriteLine("-------------------------------------自定义-------------------------------");
                List<UserSimple> listnew = list.MySelect<UserInfo, UserSimple>(delegate(UserInfo u) { return new UserSimple() { Name = u.Name }; });
                listnew.ForEach(us =>
                {
                    Console.WriteLine( "|" + us.Name);
                });
                Console.ReadLine();
            }
            static List<TR> MySelect<T1, TR>(this List<T1> list, Func<T1, TR> func)
            {
                List<TR> listnew = new List<TR>();
                foreach (T1 item in list)
                {
                    TR tr = func(item);
                    listnew.Add(tr);
                }
                return listnew;
            }
        }
    }
     
  • 相关阅读:
    微信小程序——微信支付
    .Net 异步方法, await async 使用
    微信小程序路过——新手不要错过哦!
    关于文件的上传。
    网页源代码的获取方法
    PCPOP多功能外挂v1.0
    网站开发步骤
    关于系统的垃圾文件
    关于容器中的控件的使用
    POP气球机
  • 原文地址:https://www.cnblogs.com/itmu89/p/7545440.html
Copyright © 2011-2022 走看看